fork download
  1. #!/usr/bin/env python
  2.  
  3. import socket
  4. import sys
  5.  
  6. HOST = "127.0.0.1"
  7. PORT = 9001
  8.  
  9. def client(n):
  10. global HOST, PORT
  11. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12. sock.connect((HOST, PORT))
  13. while n > 0:
  14. sock.send(str(n))
  15. data = sock.recv(20)
  16. sys.stdout.write("%02d: %s" % (n, data))
  17. n -= 1
  18. sock.close()
  19.  
  20. def server():
  21. global HOST, PORT
  22. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23. sock.bind((HOST, PORT))
  24. sock.listen(1)
  25. while True:
  26. conn, addr = sock.accept()
  27. conn.setblocking(1)
  28. while True:
  29. i = 0
  30. try:
  31. i = int(conn.recv(20))
  32. except:
  33. break
  34. s = ""
  35. if (i % 3) == 0:
  36. s += "fizz"
  37. if (i % 5) == 0:
  38. s += "buzz"
  39. conn.send(s + "\n")
  40. if i == 0:
  41. break
  42. conn.close()
  43.  
  44. if __name__ == '__main__':
  45. if len(sys.argv) < 2:
  46. print("Usage: %s <client|server> [number]" % sys.argv[0])
  47. exit(1)
  48. if sys.argv[1] == "client":
  49. if len(sys.argv) < 3:
  50. print("Usage: %s <client|server> [number]" % sys.argv[0])
  51. exit(1)
  52. client(int(sys.argv[2]))
  53. elif sys.argv[1] == "server":
  54. server()
  55. else:
  56. print("Usage: %s <client|server> [number]" % sys.argv[0])
  57. exit(1)
  58.  
Runtime error #stdin #stdout 0.02s 10352KB
stdin
Standard input is empty
stdout
Usage: prog <client|server> [number]