Created
January 4, 2019 21:23
-
-
Save homoluctus/8f30fff4eebe78b714bc35e66332bf9a to your computer and use it in GitHub Desktop.
Socket I/O Multiplexing
This file contains 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 sys | |
import socket | |
from time import sleep | |
from threading import Thread | |
SERVER = '127.0.0.1' | |
PORT = 9999 | |
def client(timeout=0): | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | |
try: | |
sock.connect((SERVER, PORT)) | |
except: | |
sys.exit(sys.exc_info()[1]) | |
try: | |
if sock.sendall(b'hello') is not None: | |
sock.shutdown(socket.SHUT_WR) | |
except: | |
sys.exit(sys.exc_info()[1]) | |
else: | |
sleep(timeout) | |
if __name__ == '__main__': | |
c1 = Thread(target=client, args=(3,)) | |
c2 = Thread(target=client, args=(1,)) | |
c3 = Thread(target=client) | |
c1.start() | |
c2.start() | |
c3.start() |
This file contains 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 sys | |
import socket | |
import select | |
HOST = '127.0.0.1' | |
PORT = 9999 | |
fd_to_socket = {} | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | |
sock.bind((HOST, PORT)) | |
sock.listen(5) | |
print("Server is listening at {}:{}".format(HOST, PORT)) | |
sock.setblocking(False) | |
poll = select.poll() | |
poll.register(sock, select.POLLIN) | |
try: | |
while True: | |
ready = poll.poll() | |
if not ready: | |
break | |
for fd, event in ready: | |
if fd == sock.fileno(): | |
connection, client_address = sock.accept() | |
print("Accepted from", client_address) | |
fd_to_socket[connection.fileno()] = connection | |
poll.register(connection, select.POLLIN) | |
else: | |
connection = fd_to_socket[fd] | |
raw_data = connection.recv(1024) | |
if not raw_data: | |
print("Closed connection to", connection.getpeername()) | |
poll.unregister(connection) | |
connection.close() | |
break | |
print("Reveived {} from {}".format(raw_data.decode(), connection.getpeername())) | |
except KeyboardInterrupt: | |
print("EXIT") | |
except: | |
print(sys.exc_info()[1]) |
This file contains 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 sys | |
import socket | |
import selectors | |
HOST = '127.0.0.1' | |
PORT = 9999 | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | |
sock.bind((HOST, PORT)) | |
sock.listen(5) | |
print("Server is listening at {}:{}".format(HOST, PORT)) | |
sock.setblocking(False) | |
with selectors.DefaultSelector() as selector: | |
selector.register(sock, selectors.EVENT_READ) | |
try: | |
while True: | |
ready = selector.select() | |
if not ready: | |
break | |
for key, _ in ready: | |
if key.fileobj == sock: | |
try: | |
connection, client_address = sock.accept() | |
print("Accepted from", client_address) | |
except: | |
sys.exc_info()[1] | |
else: | |
selector.register(connection, selectors.EVENT_READ) | |
else: | |
connection = key.fileobj | |
raw_data = connection.recv(1024) | |
if not raw_data: | |
print("Closed connection to", connection.getpeername()) | |
selector.unregister(connection) | |
connection.close() | |
break | |
print("Received {} from {}".format(raw_data.decode(), connection.getpeername())) | |
except KeyboardInterrupt: | |
print("EXIT") | |
except: | |
sys.exc_info()[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment