""" server.py A simple implementation of a server. The server's response protocol is implemented elsewhere in a handle_connection(socket) method; see ./sgg.py for an example. Tested with python 3 ; see https://docs.python.org/3.5/howto/sockets.html . Jim Mahoney | cs.marlboro.college | MIT License | April 2018 """ # python system libraries import socket, _thread # server protocol - change this to implement a new protocol from sgg import handle_connection def run_server(port=12468, max_connections=3): ip = '127.0.0.1' # Note: 127.0.0.1 is the 'localhost' address that allows processes # on this machine to connect, which is enough for demos & tests. # To run this server/client across the internet, this will need to be # the machine's network IP address. # But under MacOS, the bind() operation may throw error 49's using # real IP addresses; see for example # codefromabove.com/quickies/osx-cant-assign-requested-address-code49 . # The IP can be found with socket.gethostbyname_ex(socket.gethostname()) . serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind((ip, port)) serversocket.listen(max_connections) print("sever: listening on {}:{}.".format(ip, port)) while True: print("server: waiting for connection ...") (clientsocket, address) = serversocket.accept() print("server: connection from {}:{}\n".format(address, clientsocket)) _thread.start_new_thread(handle_connection, (clientsocket,)) if __name__ == '__main__': run_server()