python实现的简单的socket通信的客户端和服务器端,pythonsocket,服务端代码#!/usr/


服务端代码

#!/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.s.bind((host, port))        # Bind to the ports.listen(5)                 # Now wait for client connection.while True:   c, addr = s.accept()     # Establish connection with client.   print 'Got connection from', addr   c.send('Thank you for connecting')   c.close()                # Close the connection
                                客户端代码
#!/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.s.connect((host, port))print s.recv(1024)s.close                     # Close the socket when done

评论关闭