Skip to content

Instantly share code, notes, and snippets.

@hightemp
Last active May 8, 2017 17:00
Show Gist options
  • Save hightemp/cc4e130a14c7b55d7a56a28c03bd6e8c to your computer and use it in GitHub Desktop.
Save hightemp/cc4e130a14c7b55d7a56a28c03bd6e8c to your computer and use it in GitHub Desktop.
[Python] [Socket] [Queue] Example of sockets server
def fnServer(port, N=10):
s = socket.socket()
s.bind(('0.0.0.0', port))
s.listen(500)
q = Queue()
for x in xrange(N):
t = threading.Thread(target=fnThreadWorker, args=(q,))
t.daemon = True
t.start()
print 'Ready and waiting with %d threads on port %d' % (
N, port)
while True:
cli, addr = s.accept()
q.put(cli)
def fnThreadWorker(q):
while True:
sock = q.get()
handle_request(sock, time.sleep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment