November 2018
Intermediate to advanced
446 pages
12h 11m
English
Now, let's take a look at the full implementation of this server in the Chapter18/example3.py file:
# Chapter18/example3.pyimport socketfrom operator import mulfrom functools import reduce# Main event loopdef reactor(host, port): sock = socket.socket() sock.bind((host, port)) sock.listen(5) print(f'Server up, running, and waiting for call on {host} {port}') try: while True: conn, cli_address = sock.accept() process_request(conn, cli_address) finally: sock.close()def process_request(conn, cli_address): file = conn.makefile() print(f'Received connection from {cli_address}') mode = 'sum' try: conn.sendall(b'<welcome: starting in sum mode>\n') while True: line = file.readline() if line: line = line.rstrip() ...Read now
Unlock full access