#!/usr/bin/env python
import socket
import sys
HOST = "127.0.0.1"
PORT = 9001
def client(n):
global HOST, PORT
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
while n > 0:
sock.send(str(n))
data = sock.recv(20)
sys.stdout.write("%02d: %s" % (n, data))
n -= 1
sock.close()
def server():
global HOST, PORT
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(1)
while True:
conn, addr = sock.accept()
conn.setblocking(1)
while True:
i = 0
try:
i = int(conn.recv(20))
except:
break
s = ""
if (i % 3) == 0:
s += "fizz"
if (i % 5) == 0:
s += "buzz"
conn.send(s + "\n")
if i == 0:
break
conn.close()
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: %s <client|server> [number]" % sys.argv[0])
exit(1)
if sys.argv[1] == "client":
if len(sys.argv) < 3:
print("Usage: %s <client|server> [number]" % sys.argv[0])
exit(1)
client(int(sys.argv[2]))
elif sys.argv[1] == "server":
server()
else:
print("Usage: %s <client|server> [number]" % sys.argv[0])
exit(1)
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uCgppbXBvcnQgc29ja2V0CmltcG9ydCBzeXMKCkhPU1QgPSAiMTI3LjAuMC4xIgpQT1JUID0gOTAwMQoKZGVmIGNsaWVudChuKToKCWdsb2JhbCBIT1NULCBQT1JUCglzb2NrID0gc29ja2V0LnNvY2tldChzb2NrZXQuQUZfSU5FVCwgc29ja2V0LlNPQ0tfU1RSRUFNKQoJc29jay5jb25uZWN0KChIT1NULCBQT1JUKSkKCXdoaWxlIG4gPiAwOgoJCXNvY2suc2VuZChzdHIobikpCgkJZGF0YSA9IHNvY2sucmVjdigyMCkKCQlzeXMuc3Rkb3V0LndyaXRlKCIlMDJkOiAlcyIgJSAobiwgZGF0YSkpCgkJbiAtPSAxCglzb2NrLmNsb3NlKCkKCmRlZiBzZXJ2ZXIoKToKCWdsb2JhbCBIT1NULCBQT1JUCglzb2NrID0gc29ja2V0Ln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