Python无阻塞SSL套接字展开:如何检测关闭握手的完成?

时间:2018-11-02 22:26:08

标签: python sockets python-asyncio

给出一个无阻塞的套接字...

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()

0 个答案:

没有答案