Python网络编程:多连接模型与SSL安全通信
1. 多连接的客户端 - 服务器模型
在Python 3.4及以上版本中,有一个名为 selectors 的模块,它基于I/O原语提供了一个用于快速构建面向对象服务器的API。文档和示例可参考 https://docs.python.org/3.7/library/selectors.html 。
1.1 服务器实现
以下是 tcp_server_selectors.py 文件中的代码:
#!/usr/bin/env python3
import selectors
import types
import socket
selector = selectors.DefaultSelector()
def accept_connection(sock):
connection, address = sock.accept()
print('Connection accepted in {}'.format(address))
# We put the socket in non-blocking mode
connection.setblocking(False)
data = types.SimpleNamespace(addr=address, inb=b'', outb=b'')
超级会员免费看
订阅专栏 解锁全文
1668

被折叠的 条评论
为什么被折叠?



