Python一个简单的通信程序(客户端 服务器)攻略
概述
本文主要介绍如何使用Python语言编写一个简单的通信程序,包括客户端和服务器端。通过使用Python内置的socket库,可以很方便地实现网络通信功能。
步骤
客户端
- 导入socket库,并创建一个socket对象。
```python
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
```
上述代码中socket.AF_INET指定了地址族为IPv4,socket.SOCK_STREAM指定套接字类型为流式套接字(TCP协议)。
- 连接服务器。首先需要指定服务器的IP地址和端口号,然后使用socket.connect()方法连接服务器。
python
server_address = ('127.0.0.1', 8888)
client_socket.connect(server_address)
这里将服务器地址设为本机地址127.0.0.1,端口号为8888,可以根据实际情况进行调整。
- 发送数据。使用socket.send()方法向服务器发送数据。
python
message = 'Hello, server!'
client_socket.send(message.encode())
在本例中,将字符串“Hello, server!”按照UTF-8编码格式转换为字节流发送给服务器。
- 接收数据。使用socket.recv()方法从服务器接收数据。
python
data = client_socket.recv(1024)
print('Received from server:', data.decode())
在本例中,接收缓冲区大小为1024字节,接收到的数据使用UTF-8格式解码后输出。
- 关闭连接。通信结束后,一定要关闭套接字。
python
client_socket.close()
服务器
- 导入socket库,并创建一个socket对象。
```python
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
```
同样,这里指定了地址族为IPv4,套接字类型为流式套接字(TCP协议)。
- 绑定地址和端口号。使用socket.bind()方法将服务器的地址和端口号绑定到套接字上。
python
server_address = ('127.0.0.1', 8888)
server_socket.bind(server_address)
- 监听客户端连接。使用socket.listen()方法开始监听客户端的连接请求。
python
server_socket.listen(5)
在本例中,设置最大连接客户端数量为5。
- 接受客户端连接。使用socket.accept()方法接受客户端的连接请求。
python
client_socket, client_address = server_socket.accept()
print('Client connected:', client_address)
当有客户端连接到服务器时,将返回一个新的套接字和客户端地址。
- 接收数据。使用socket.recv()方法从客户端接收数据。
python
data = client_socket.recv(1024)
print('Received from client:', data.decode())
在本例中,接收缓冲区大小为1024字节,接收到的数据使用UTF-8格式解码后输出。
- 发送数据。使用socket.send()方法向客户端发送数据。
python
message = 'Hello, client!'
client_socket.send(message.encode())
在本例中,将字符串“Hello, client!”按照UTF-8编码格式转换为字节流发送给客户端。
- 关闭连接。通信结束后,一定要关闭套接字。
python
client_socket.close()
server_socket.close()
示例
示例1:客户端发送数据,服务器接收并返回
客户端代码:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.0.1', 8888)
client_socket.connect(server_address)
message = 'Hello, server!'
client_socket.send(message.encode())
data = client_socket.recv(1024)
print('Received from server:', data.decode())
client_socket.close()
服务器端代码:
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.0.1', 8888)
server_socket.bind(server_address)
server_socket.listen(5)
client_socket, client_address = server_socket.accept()
print('Client connected:', client_address)
data = client_socket.recv(1024)
print('Received from client:', data.decode())
message = 'Hello, client!'
client_socket.send(message.encode())
client_socket.close()
server_socket.close()
运行以上代码,在客户端控制台输出:
Received from server: Hello, client!
在服务器控制台输出:
Client connected: ('127.0.0.1', 57410)
Received from client: Hello, server!
示例2:客户端发送多条数据,服务器分别接收并返回
客户端代码:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.0.1', 8888)
client_socket.connect(server_address)
messages = ['foo', 'bar', 'baz', 'qux']
for message in messages:
client_socket.send(message.encode())
data = client_socket.recv(1024)
print('Received from server:', data.decode())
client_socket.close()
服务器端代码:
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.0.1', 8888)
server_socket.bind(server_address)
server_socket.listen(5)
client_socket, client_address = server_socket.accept()
print('Client connected:', client_address)
while True:
data = client_socket.recv(1024)
if not data:
break
print('Received from client:', data.decode())
message = 'ACK ' + data.decode()
client_socket.send(message.encode())
client_socket.close()
server_socket.close()
运行以上代码,在客户端控制台输出:
Received from server: ACK foo
Received from server: ACK bar
Received from server: ACK baz
Received from server: ACK qux
在服务器控制台输出:
Client connected: ('127.0.0.1', 57412)
Received from client: foo
Received from client: bar
Received from client: baz
Received from client: qux
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python一个简单的通信程序(客户端 服务器) - Python技术站