给出一个无阻塞的套接字...
import asyncio
import socket
server_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=socket.IPPROTO_TCP)
server_sock.setblocking(False)
server_sock.bind(('', port))
server_sock.listen(socket.IPPROTO_TCP)
loop = asyncio.get_event_loop()
sock, _ = await loop.sock_accept(server_sock)
...包裹有ssl ...
import ssl
ssl_context = ssl.SSLContext(PROTOCOL_TLSv1_2)
ssl_context.load_cert_chain(f'ssl.crt', keyfile=f'ssl.key')
ssl_sock = ssl_context.wrap_socket(sock, server_side=True, do_handshake_on_connect=False)
...,并且已经由do_handshake
执行了设置握手和循环操作...
while True:
try:
ssl_sock.do_handshake()
break
except (ssl.SSLWantReadError, ssl.SSLWantWriteError):
# We could have something more efficient, e.g. doing things
# with the readers/writers, but it works
await asyncio.sleep(0)
...,然后调用unwrap
方法以启动关闭握手...
try:
ssl_sock.unwrap()
except (SSLWantReadError, SSLWantWriteError):
pass
...那么我如何等待关闭握手完成?所以我可以写类似...
await ssl_shutdown_handshake_complete()
?