客户端
1 #!/usr/bin/env python 2 #coding:utf-8 3 4 import socket 5 6 client = socket.socket() 7 8 ip_port=('127.0.0.1',9999) 9 client.connect(ip_port) 10 11 while True: 12 data=client.recv(1024)#接收数据 13 print data 14 inp=raw_input('information:')#发送数据 15 client.send(inp) 16 if inp == 'exit': 17 break
服务器端
1 #!/usr/bin/env python 2 #coding:utf-8 3 4 import socket 5 6 sk = socket.socket() 7 8 ip_port=('127.0.0.1',9999) 9 sk.bind(ip_port) 10 sk.listen(5) 11 12 while True: 13 conn,address = sk.accept()#返回值是元组,conn相当于一个会话标识 14 conn.send('hello,world') 15 flag=True 16 while flag: 17 data=conn.recv(1024) 18 print data 19 if data == 'exit': 20 flag=False 21 conn.send('sb') 22 conn.close()