Skip to content

Instantly share code, notes, and snippets.

@bhavishyagopesh
Last active August 17, 2017 19:18
Show Gist options
  • Save bhavishyagopesh/1608695251123b6d85fc3a59c7da022a to your computer and use it in GitHub Desktop.
Save bhavishyagopesh/1608695251123b6d85fc3a59c7da022a to your computer and use it in GitHub Desktop.
# server code
from fib import fib
from socket import *
def fib_server(address):
# This sets up the socket(you can think that of as pipe over which communication happens) object
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# It binds to the address and listens for any connections
sock.bind(address)
sock.listen(5)
while True:
client, addr = sock.accept()
print("Connection", addr)
fib_handler(client)
# This function handles the connection and sends back the fib() value
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