>>> import socket
>>> s=socket.socket()
>>> hostname=socket.gethostname()
>>> port=5566
>>> s.bind((hostname,port))
>>> s.listen(5)
>>> while True:
... c,addr=s.accept()
... print 'Got connection from ',addr
... c.send('Thank you for connecting')
... c.close()
...
Got connection from ('192.168.2.118', 54722) # 这是客户机连接后的输出语句
24 # 这是客户机连接后的输出语句
编写好服务端后,不要关闭当前命令窗,再新开一个命令窗编写客户机
>>> import socket
>>> s=socket.socket()
>>> hostname=socket.gethostname()
>>> port=5566
>>> s.connect((hostname,port))
>>> print s.recv(1024)
Thank you for connecting
当看到命令窗输出“Thank you for connecting”,说明连接成功了。同时,服务器端窗后也有信息,会打印Got connection from 192.168.x.x