#!/usr/bin/python3 import socket import threading import random # host (internal) IP address and port HOST = "10.128.0.22" PORT = 5221 # handle one client in a thread def handle_client(connection, address): connection.sendall("Welcome to the guess the number game!\n".encode()) connection.sendall("Think of a number 1 to 1000!\n: ".encode()) secret = random.randint(1, 1000) while True: try: number = int(connection.recv(1024).decode()) if number == secret: connection.sendall("Correct! Goodbye!\n".encode()) break elif number < secret: connection.sendall("Too low, try again\n: ".encode()) else: connection.sendall("Too high, try again\n: ".encode()) except: connection.sendall("Invalid input. Goodbye!\n".encode()) break connection.close() def main(): # listen on the port specified sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((HOST, PORT)) sock.listen() # wait forever gathering clients while True: connection, address = sock.accept() print("Received a connection from", address) t = threading.Thread(target=handle_client, args=(connection,address)) t.start(); main()