Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Client
- import socket
- import struct
- import random
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((socket.gethostname(), 1032))
- guess = 0
- while guess == 0:
- n = random.randint(0, 10)
- print(f"The client's number{n}")
- s.send(struct.pack("!H", n))
- nr = s.recv(2)
- guess = struct.unpack("!H", nr)[0]
- print(f"Guess: {guess}")
- nr = s.recv(2)
- nr = struct.unpack("!H", nr)
- fullMsg = ""
- for i in range(nr):
- msg = s.recv(1)
- msg = msg.decode("ascii")
- fullMsg += msg
- print(fullMsg)
- #Server
- import socket
- import struct
- import random
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.bind((socket.gethostname(), 1032))
- s.listen()
- tries = 0
- while True:
- connection, address = s.accept()
- try:
- guess = 0
- while(guess == 0):
- n = connection.recv(2)
- n = struct.unpack("!H", n)[0]
- print(f"The client's number{n}")
- x = random.randint(0, 10)
- print(f"The server's number{x}")
- if x == n:
- guess = 1
- msg = "You win - within " + str(tries) + " tries"
- connection.send("!H", guess)
- connection.send(struct.pack("!H", len(msg)))
- connection.send(bytes(msg, "ascii"))
- else:
- tries += 1
- finally:
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement