Created
December 7, 2017 12:53
-
-
Save xieyuheng/c4d7c3f59dc982d41bc0338ac3c7f9e1 to your computer and use it in GitHub Desktop.
fib server
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
def fib(n): | |
if n <= 2: | |
return 1 | |
else: | |
return fib(n-1) + fib(n-2) |
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
from socket import * | |
from fib import fib | |
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("- fib_server") | |
print(" accept connection from address : {}".format(addr)) | |
fib_handler(client) | |
def fib_handler(client): | |
while True: | |
req = client.recv(100) | |
if not req: | |
break | |
n = int(req) | |
result = fib(n) | |
resp = (str(result) + '\n').encode('utf-8') | |
client.send(resp) | |
print("- fib_handler") | |
print(" close") | |
fib_server(('', 25000)) |
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
from socket import * | |
def nc(): | |
sock = socket(AF_INET, SOCK_STREAM) | |
sock.connect(('localhost', 25000)) | |
while True: | |
n = input() | |
sock.send(n.encode('utf-8')) | |
resp = sock.recv(100) | |
print(resp.decode('utf-8'), end='') | |
nc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment