Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import struct
- import random
- import time
- HOST = "192.168.43.238"
- PORT = 19999
- def readInt(sock):
- data = sock.recv(4)
- data = struct.unpack("!i", data)[0]
- return data
- def sendInt(sock, toSend):
- sock.send(struct.pack("!i", toSend))
- def sendChar(sock, toSend):
- sock.send(struct.pack("!c", toSend.encode('ascii')))
- def readChar(sock):
- data = sock.recv(1)
- data = struct.unpack("!c", data)[0].decode('ascii')
- return data
- def sendIntArray(sock, toSend):
- sendInt(sock, len(toSend))
- for value in toSend:
- sendInt(sock, value)
- def sendCharArray(sock, toSend):
- sendInt(sock, len(toSend))
- for value in toSend:
- sendChar(sock, value)
- def readIntArray(sock):
- buff = []
- length = readInt(sock)
- for i in range(length):
- buff.append(readInt(sock))
- return buff
- def readCharArray(sock):
- buff = ""
- length = readInt(sock)
- for i in range(length):
- buff+=readChar(sock)
- return buff
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
- sock.connect((HOST, PORT))
- mins = 0
- tops = 1000
- while True:
- guess = random.randint(mins, tops)
- time.sleep(0.1)
- print(guess)
- sendInt(sock, guess)
- answer = readCharArray(sock)
- if answer == "Lower":
- tops = guess
- elif answer == "Higher":
- mins = guess
- else:
- print(answer)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement