Created
August 17, 2017 17:25
-
-
Save bhavishyagopesh/cdff5833bdbe3bddeff2cdc09f133307 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
# threaded-server code | |
from fib import fib | |
from socket import * | |
from threading import Thread | |
def fib_server(address): | |
sock = socket(AF_INET, SOCK_STREAM) | |
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) | |
sock.bind(address) | |
sock.listen(5) | |
while True: | |
client, addr = sock.accept() | |
print("Connection", addr) | |
Thread(target=fib_handler, args=(client,), daemon=True).start() | |
def fib_handler(client): | |
while True: | |
req = client.recv(100) | |
if not req: | |
break | |
n = int(req) | |
result = fib(n) | |
resp = str(result).encode('ascii') + b'\n' | |
client.send(resp) | |
print("Closed") | |
# This starts a server that listens to localhost on port 25000 | |
fib_server(('', 25000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment