下面是详细的攻略:
简介
C/S模式是计算机网络中常见的模式之一,它指的是Client-Server模式,即客户端-服务器模式。在这种模式下,服务器端提供服务,客户端向服务器发起请求并接受服务。在本攻略中,我们将使用Python实现一个简单的C/S模型。
基础知识
在开始之前,我们需要掌握以下基础知识:
- socket模块:用于实现网络通信;
- threading模块:用于实现多线程。
服务器端
-
创建一个socket对象,指定网络通信协议和类型。
python
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -
绑定IP地址和端口号。
python
ip = '127.0.0.1' # 本地IP地址
port = 8800 # 端口号
server_socket.bind((ip, port)) -
设置监听,等待客户端连接。
python
server_socket.listen(5) # 最多允许5个客户端连接 -
定义一个多线程函数,用于接收来自客户端的消息,并处理。
```python
import threadingdef handle_client(client_socket, client_address):
while True:
try:
data = client_socket.recv(1024) # 接收消息
print('{}:{} - {}'.format(client_address[0], client_address[1], data.decode('utf-8'))) # 打印消息
except:
print('{}:{}退出!'.format(client_address[0], client_address[1])) # 客户端关闭连接
client_socket.close() # 关闭连接
break
``` -
循环监听客户端连接,每当有新的客户端连入时,启动一个线程处理该客户端的消息。
python
while True:
client_socket, client_address = server_socket.accept() # 接收客户端连接
print('{}:{}已连接!'.format(client_address[0], client_address[1]))
t = threading.Thread(target=handle_client, args=(client_socket, client_address)) # 启动一个线程处理该客户端的消息
t.start() -
完整代码实例请参考以下示例:
```python
import socket
import threadingdef handle_client(client_socket, client_address):
while True:
try:
data = client_socket.recv(1024)
print('{}:{} - {}'.format(client_address[0], client_address[1], data.decode('utf-8')))
except:
print('{}:{}退出!'.format(client_address[0], client_address[1]))
client_socket.close()
breakserver_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
port = 8800
server_socket.bind((ip, port))
server_socket.listen(5)
print('服务器已启动......')
while True:
client_socket, client_address = server_socket.accept()
print('{}:{}已连接!'.format(client_address[0], client_address[1]))
t = threading.Thread(target=handle_client, args=(client_socket, client_address))
t.start()
```
客户端
-
创建一个socket对象,用于连接服务器端。
python
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -
连接服务器。
python
ip = '127.0.0.1' # 服务器IP地址
port = 8800 # 端口号
client_socket.connect((ip, port)) -
发送消息给服务器端。
python
message = 'Hello, world!'
client_socket.send(message.encode('utf-8')) -
完整代码实例请参考以下示例:
python
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
port = 8800
client_socket.connect((ip, port))
message = 'Hello, world!'
client_socket.send(message.encode('utf-8'))
client_socket.close()
示例说明
以下是两个示例:
示例1
服务器端:
import socket
import threading
def handle_client(client_socket, client_address):
while True:
try:
data = client_socket.recv(1024)
print('{}:{} - {}'.format(client_address[0], client_address[1], data.decode('utf-8')))
except:
print('{}:{}退出!'.format(client_address[0], client_address[1]))
client_socket.close()
break
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
port = 8800
server_socket.bind((ip, port))
server_socket.listen(5)
print('服务器已启动......')
while True:
client_socket, client_address = server_socket.accept()
print('{}:{}已连接!'.format(client_address[0], client_address[1]))
t = threading.Thread(target=handle_client, args=(client_socket, client_address))
t.start()
客户端:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
port = 8800
client_socket.connect((ip, port))
message = 'Hello, world!'
client_socket.send(message.encode('utf-8'))
client_socket.close()
在两个控制台中分别启动上述代码,可以看到客户端向服务器端发送了一条消息,并在服务器端控制台上打印了信息。
示例2
服务器端:
import socket
import threading
def handle_client(client_socket, client_address):
while True:
try:
data = client_socket.recv(1024)
print('{}:{} - {}'.format(client_address[0], client_address[1], data.decode('utf-8')))
except:
print('{}:{}退出!'.format(client_address[0], client_address[1]))
client_socket.close()
break
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
port = 8800
server_socket.bind((ip, port))
server_socket.listen(5)
print('服务器已启动......')
while True:
client_socket, client_address = server_socket.accept()
print('{}:{}已连接!'.format(client_address[0], client_address[1]))
t = threading.Thread(target=handle_client, args=(client_socket, client_address))
t.start()
客户端:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
port = 8800
client_socket.connect((ip, port))
message = input('请输入要发送的消息:')
client_socket.send(message.encode('utf-8'))
client_socket.close()
在客户端控制台中输入一条消息,可以看到服务器端控制台上打印出了该消息。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于python实现简单C/S模式代码实例 - Python技术站