Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import sys
- import time
- import os
- from random import randint
- #from signal import signal, SIGPIPE, SIG_DFL
- #signal(SIGPIPE, SIG_DFL)
- # Create a TCP/IP socket
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # Bind the socket to the port
- sock.bind(('', int(sys.argv[1])))
- # Listen for incoming connections
- sock.listen(2)
- while True:
- # Wait for a connection
- print ('Waiting for a connection')
- connection, client_address = sock.accept()
- newPid = os.fork()
- if newPid == 0:
- print (client_address)
- nr_rand = randint(0,100)
- print(nr_rand)
- data = -1
- nr_tries = 1
- while data!=nr_rand:
- data = connection.recv(1024)
- data = data.split("\n")
- data = data[0]
- print(str(data))
- data = int(data)
- if data < nr_rand:
- connection.send("<"+"\0")
- nr_tries+=1
- elif data > nr_rand:
- connection.send(">"+"\0")
- nr_tries+=1
- else:
- connection.send("="+"\0")
- time.sleep(0.5)
- connection.sendall(str(nr_tries)+"\0")
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement