pyOpenSSL 和 WantReadError
我有一个套接字服务器,我试图将其转移到 python 2.5 上的 SSL,但我遇到了 pyOpenSSL 的障碍。我找不到任何关于使用它的好的教程,所以我主要靠猜测进行操作。
这是我的服务器如何设置套接字:
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.use_privatekey_file ("mykey.pem")
ctx.use_certificate_file("mycert.pem")
sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
addr = ('', int(8081))
sock.bind(addr)
sock.listen(5)
这是它如何接受客户端:
sock.setblocking(0)
while True:
if len(select([sock], [], [], 0.25)[0]):
client_sock, client_addr = sock.accept()
client = ClientGen(client_sock)
这是它如何从连接的套接字发送/接收:
while True:
(r, w, e) = select.select([sock], [sock], [], 0.25)
if len(r):
bytes = sock.recv(1024)
if len(w):
n_bytes = sock.send(self.message)
它是压缩的,但您可以了解总体思路。问题是,一旦发送/接收循环开始,它会在发送或接收任何内容之前立即终止(无论如何我都能看到):
Traceback (most recent call last):
File "ClientGen.py", line 50, in networkLoop
n_bytes = sock.send(self.message
WantReadError
手册对“WantReadError”的描述非常模糊,说它可能来自大约任何地方。我做错了什么?
I have a socket server that I am trying to move over to SSL on python 2.5, but I've run into a snag with pyOpenSSL. I can't find any good tutorials on using it, so I'm operating largely on guesses.
Here is how my server sets up the socket:
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.use_privatekey_file ("mykey.pem")
ctx.use_certificate_file("mycert.pem")
sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
addr = ('', int(8081))
sock.bind(addr)
sock.listen(5)
Here is how it accepts clients:
sock.setblocking(0)
while True:
if len(select([sock], [], [], 0.25)[0]):
client_sock, client_addr = sock.accept()
client = ClientGen(client_sock)
And here is how it sends/receives from the connected sockets:
while True:
(r, w, e) = select.select([sock], [sock], [], 0.25)
if len(r):
bytes = sock.recv(1024)
if len(w):
n_bytes = sock.send(self.message)
It's compacted, but you get the general idea. The problem is, once the send/receive loop starts, it dies right away, before anything has been sent or received (that I can see anyway):
Traceback (most recent call last):
File "ClientGen.py", line 50, in networkLoop
n_bytes = sock.send(self.message
WantReadError
The manual's description of the 'WantReadError' is very vague, saying it can come from just about anywhere. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有时,为了发送 SSL 连接的应用程序字节,您需要能够首先从连接读取更多字节。
WantReadError
是这种情况的指示方式。您做错的唯一一件事是您没有处理WantReadError
,然后等待select
指示套接字可读之前您尝试再次调用send
。Sometimes in order to send application bytes of an SSL connection, you need to be able to read more bytes from the connection first.
WantReadError
is how this case is indicated. The only thing you're doing wrong is that you're not handling theWantReadError
and then waiting untilselect
indicates that the socket is readable before you try callingsend
again.