Here is a server utilizing the HasyUtils.sockel class. The function socketAcceptor is constantly waiting for new connects. Threads are spawned for each client.
The mirror server:
#!/usr/bin/env python import HasyUtils import thread, os PORT = 7650 def socketAcceptor(): # waits for new accepts on the original socket, # receives the newly created socket and # creates threads to handle each client separatly while True: s = HasyUtils.sockel( HasyUtils.getHostname(), PORT) thread.start_new_thread( socketServer, (s, )) def socketServer(s): global msgBuf while True: msg = s.recv() if msg is None: print( "received None, closing socket") s.close() break if msg.find('exit') >= 0: os._exit(1) s.send( ">> %s" % msg.strip()) if __name__ == "__main__": socketAcceptor()
A client:
#!/usr/bin/env python import socket import sys def main(): host = socket.gethostname() port = 7650 sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # # find the port where the server is accepting connects # for i in range(10): try: sckt.connect((host, port)) except Exception as e: print( "connect() failed", e) port += 1 continue break else: print( "connect() failed") sckt.close() sys.exit() print( "talking via port %d" % port) print( "say 'bye' to finish") while( 1): sys.stdout.write( "Enter: ") msg = sys.stdin.readline().strip() if msg.lower() == "bye" : break sckt.send( msg) data = sckt.recv(1024) print( 'Received %s' % repr(data)) sckt.close() return if __name__ == "__main__": main()