Created
April 18, 2012 03:53
-
-
Save aji/2411006 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import select | |
| listener = socket.socket() | |
| listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
| listener.bind(('', 9001)) | |
| listener.listen(5) | |
| running = True | |
| conns = [] | |
| while running: | |
| try: | |
| r, w, x = [listener] + conns, [], [] | |
| r, w, x = select.select(r, w, x) | |
| for sock in r: | |
| if sock == listener: | |
| conn, addr = sock.accept() | |
| conns.append(conn) | |
| print('Accepted from {0}'.format(addr[0])) | |
| else: | |
| data = sock.recv(2048) | |
| if data == b'': | |
| print(' Client gave up') | |
| sock.close() | |
| conns.remove(sock) | |
| except socket.error: | |
| running = False | |
| for sock in conns: | |
| sock.close() | |
| listener.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment