Skip to content

Instantly share code, notes, and snippets.

@jrziviani
Created October 1, 2013 01:36
Show Gist options
  • Save jrziviani/6772809 to your computer and use it in GitHub Desktop.
Save jrziviani/6772809 to your computer and use it in GitHub Desktop.
Servidor simples que aceita várias conexões de clientes ao mesmo tempo usando select.
import select
import socket
import os
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serv.bind(('', 9090))
serv.listen(1)
sockets = [serv]
while True:
# mantem todos os sockets abertos em escuta
read_input, _, _ = select.select(sockets, [], [])
# para cada um dos sockets prontos para leitura
for socket in read_input:
# aceita uma nova conexao caso o socket para leitura
# seja o servidor
if socket == serv:
sockets.append(socket.accept()[0])
# imprime os dados caso o socket para leitura seja
# o cliente
else:
d = socket.recv(1024)[:-2]
if d == 'q':
socket.close()
sockets.remove(socket)
else:
print d
# encerra caso nao tenha mais cliente conectados
if len(sockets) == 1:
break
serv.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment