I got a good example for server client in python.
Server :
Client :
Reference :
http://www.dscripts.net/2010/06/18/how-to-create-a-client-server-socket-connection-in-python/
Server :
#!/usr/bin/python # This is server.py fileimport socket # Import socket modules = socket.socket() # Create a socket objecthost = socket.gethostname() # Get local machine nameport = 12345 # Reserve a port for your service. print 'Server started!'print 'Waiting for clients...'s.bind((host, port)) # Bind to the ports.listen(5) # Now wait for client connection.c, addr = s.accept() # Establish connection with client.print 'Got connection from', addrwhile True: msg = c.recv(1024) print addr, ' >> ', msg msg = raw_input('SERVER >> ') c.send(msg); #c.close() # Close the connectionClient :
#!/usr/bin/python # This is client.py fileimport socket # Import socket modules = socket.socket() # Create a socket objecthost = socket.gethostname() # Get local machine nameport = 12345 # Reserve a port for your service.print 'Connecting to ', host, ports.connect((host, port))while True: msg = raw_input('CLIENT >> ') s.send(msg) msg = s.recv(1024) print 'SERVER >> ', msg#s.close # Close the socket when doneReference :
http://www.dscripts.net/2010/06/18/how-to-create-a-client-server-socket-connection-in-python/

No comments:
Post a Comment