建立server:

#!/usr/bin/env python
#coding=utf-8
#File: SimServer.py
#
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 1051))
server.listen(5)
client, addr = server.accept()
#print ("Got connected from Ip = %s " % addr)
print "Got connected from Ip", addr
data = client.recv(1024)
print ("Got data = %s" % data)
client.send('I GOT: %s' % data)
server.close()

建立client:

#!/usr/bin/env python
#coding=utf-8
#File: SimClient.py
#
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
   client.connect(('192.168.1.3', 1051))
except:
   (ErrorType, ErrorValue, ErrorTB) = sys.exc_info()
   print "Connect server failed: ", ErrorValue
  
client.send('hello')
data = client.recv(512)
print ("Got data = %s" % data)
client.close()