server
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socket 5 6 # AF_INET IPv4 SOCK_STREAM TCP协议 7 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 server.bind(('0.0.0.0', 7777)) 9 server.listen() 10 sock, addr = server.accept() 11 12 # 获取从客户端发送的数据,一次获取1K的数据 13 data = sock.recv(1024) 14 print(data.decode('utf-8')) 15 sock.send('hello {}'.format(data.decode('utf-8')).encode('utf-8')) 16 sock.close() 17 server.close()
client
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socket 5 6 7 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 client.connect(('127.0.0.1',7777)) 9 client.send('zy'.encode('utf-8')) 10 data = client.recv(1024) 11 print(data.decode('utf-8')) 12 client.close()
server
zy
client
hello zy