一、基本socket例子
服务端:
1 import socket 2 3 HOST = '' 4 PORT = 50007 5 6 sock_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 sock_server.bind((HOST, PORT)) 8 9 sock_server.listen(1) 10 conn, addr = sock_server.accept() 11 12 with conn: 13 print('Connected by', addr) 14 while True: 15 data = conn.recv(1024) 16 if not data: 17 break 18 conn.sendall(data)
客户端:
1 import socket 2 3 HOST = 'localhost' 4 PORT = 50007 5 6 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 client.connect((HOST, PORT)) 8 client.sendall(b'Hello, world') 9 10 data = client.recv(1024) 11 12 print('Received', data)
先运行服务端,再运行客户端,客户端运行结果为:
1 Received b'Hello, world'
二、循环收发数据
服务端:
1 import socket 2 3 HOST = '' 4 PORT = 50007 5 6 sock_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 sock_server.bind((HOST, PORT)) 8 9 sock_server.listen(1) 10 conn, addr = sock_server.accept() 11 12 with conn: 13 print('Connected by', addr) 14 while True: 15 data = conn.recv(1024) 16 print("server recv:", conn.getpeername(), data.decode()) 17 if not data: 18 break 19 conn.sendall(data)
客户端:
1 import socket 2 3 HOST = 'localhost' 4 PORT = 50007 5 6 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 client.connect((HOST, PORT)) 8 9 while True: 10 msg = input(">>>:").strip() 11 if len(msg) == 0: 12 continue 13 client.sendall(msg.encode()) 14 15 data = client.recv(1024) 16 17 print('Received', data.decode())
先运行服务端,在运行客户端,依次在客户端输入:
hi、你好、学习python,
客户端运行结果为:
1 >>>:hi 2 Received hi 3 >>>:你好 4 Received 你好 5 >>>:学习python 6 Received 学习python 7 >>>:
服务端运行结果为:
1 Connected by ('127.0.0.1', 57473) 2 server recv: ('127.0.0.1', 57473) hi 3 server recv: ('127.0.0.1', 57473) 你好 4 server recv: ('127.0.0.1', 57473) 学习python
三、简单聊天软件
服务端
1 import socket 2 3 HOST = '' 4 PORT = 50007 5 6 sock_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 sock_server.bind((HOST, PORT)) 8 9 sock_server.listen(1) 10 conn, addr = sock_server.accept() 11 12 with conn: 13 print('Connected by', addr) 14 while True: 15 data = conn.recv(1024) 16 print("server recv:", conn.getpeername(), data.decode()) 17 if not data: 18 break 19 20 response = input(">>>:").strip() 21 conn.send(response.encode()) 22 print("send to alex:", response)
客户端
1 import socket 2 3 HOST = 'localhost' 4 PORT = 50007 5 6 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 client.connect((HOST, PORT)) 8 9 while True: 10 msg = input(">>>:").strip() 11 if len(msg) == 0: 12 continue 13 client.sendall(msg.encode()) 14 15 data = client.recv(1024) 16 17 print('Received', data.decode())
先运行服务端,再运行客户端,
客户端运行结果为:
1 >>>:Hi 2 Received hey 3 >>>:刚强 4 Received alex 5 >>>:egon在吗 6 Received 不在 7 >>>:哈哈 8 Received 哈哈 9 >>>:
服务端运行结果为:
1 Connected by ('127.0.0.1', 57682) 2 server recv: ('127.0.0.1', 57682) Hi 3 >>>:hey 4 send to alex: hey 5 server recv: ('127.0.0.1', 57682) 刚强 6 >>>:alex 7 send to alex: alex 8 server recv: ('127.0.0.1', 57682) egon在吗 9 >>>:不在 10 send to alex: 不在 11 server recv: ('127.0.0.1', 57682) 哈哈 12 >>>:哈哈 13 send to alex: 哈哈
可以看出吗,以上代码能够实现简单的聊天功能