forked from dabeaz/concurrencylive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_processes.py
More file actions
30 lines (25 loc) · 832 Bytes
/
server_processes.py
File metadata and controls
30 lines (25 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# a simple server using processes for tests
import datetime
from multiprocessing import Process
from socket import socket, SOL_SOCKET, SO_REUSEADDR, AF_INET, SOCK_STREAM
from fib import fib
def fib_server(address):
sock = socket(family=AF_INET, type=SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(5)
while True:
client, addr = sock.accept()
print(datetime.datetime.utcnow(), "Connection", addr)
Process(target=fib_handler, args=(client, addr,)).start()
def fib_handler(client, addr):
while True:
req = client.recv(100)
if not req:
break
n = int(req)
fibN = fib(n)
response = "Reponse = " + str(fibN).encode(encoding='ascii') + b'\n'
client.send(response)
print(datetime.datetime.utcnow(), addr, "Closed")
fib_server(('', 25000))