Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import socket
  2. import sys
  3. import time
  4. import os
  5.  
  6. from random import randint
  7. #from signal import signal, SIGPIPE, SIG_DFL
  8. #signal(SIGPIPE, SIG_DFL)
  9.  
  10. # Create a TCP/IP socket
  11. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12.  
  13. # Bind the socket to the port
  14. sock.bind(('', int(sys.argv[1])))
  15.  
  16. # Listen for incoming connections
  17. sock.listen(2)
  18.  
  19. while True:
  20. # Wait for a connection
  21. print ('Waiting for a connection')
  22.  
  23. connection, client_address = sock.accept()
  24. newPid = os.fork()
  25. if newPid == 0:
  26. print (client_address)
  27. nr_rand = randint(0,100)
  28. print(nr_rand)
  29. data = -1
  30. nr_tries = 1
  31. while data!=nr_rand:
  32. data = connection.recv(1024)
  33. data = data.split("\n")
  34. data = data[0]
  35. print(str(data))
  36. data = int(data)
  37. if data < nr_rand:
  38. connection.send("<"+"\0")
  39. nr_tries+=1
  40. elif data > nr_rand:
  41. connection.send(">"+"\0")
  42. nr_tries+=1
  43. else:
  44. connection.send("="+"\0")
  45.  
  46. time.sleep(0.5)
  47. connection.sendall(str(nr_tries)+"\0")
  48. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement