-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockets-server.py
More file actions
41 lines (32 loc) · 982 Bytes
/
sockets-server.py
File metadata and controls
41 lines (32 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
#Biblioteca sockets
import socket
#Porta do servidor
port = 8084
#Mensagem que o servidor envia
msg = input('Mensagem a ser enviada (SERVIDOR -> CLIENTE): ')
#Objeto socket
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Associa o socket a uma porta local
serv.bind(('0.0.0.0',port))
serv.listen()
#Servidor fica aguardando conexões
while True:
print(f'== Servidor aguardando conexões na porta {port} ==')
conn, addr = serv.accept()
print(f'== Conexao recebida de {addr} ==')
#Recebe os dados
data = conn.recv(4096)
#Caso não haja dados, encerra a conexao
if not data: break
#Trata os dados recebidos
from_client = data
print('== Dados recebidos: ==')
print(from_client.decode())
#Envia uma mensagem
print('== Enviando mensagem ==')
conn.send(from_client.upper())
print('== Mensagem enviada ==')
#Fecha a conexao
conn.close()
print('== Cliente desconectado ==')