1 from socket import *
2 #创建服务器套接字对象
3 server_socket = socket(AF_INET,SOCK_STREAM)
4 #绑定端口
5 server_socket.bind(('',8888))
6 #监听
7 server_socket.listen()
8 #等待客户端的连接
9 client_socket,client_info = server_socket.accept()
10 while True:
11 #接收客户端的消息
12 recv_data = client_socket.recv(1024)
13 print('客户端说:',recv_data.decode('utf-8'))
14 #发送消息
15 msg = input('>')
16 client_socket.sent(msg.encode('utf-8'))
17 client_socket.close()
18 server_socket.close()
1 from socket import *
2 #创建客户端套接字对象
3 client_socket=socket(AF_INET,SOCK_STREAM)
4 #调用connect方法与服务器建立连接
5 client_socket.connect(('192.168.0.107',8888))
6 while True:
7 #客户端发送消息
8 msg=input('>')
9 client_socket.send(msg.encode('utf-8'))
10 if msg == 'bye':
11 break
12 #客户端接收消息
13 recv_data=client_socket.recv(1024)
14 print('服务器端说:',recv_data.decode('utf-8'))
15 client_socket.close()