简体   繁体   English

pyOpenSSL 和 WantReadError

[英]pyOpenSSL and the 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.我有一个套接字服务器,我试图在 python 2.5 上转移到 SSL,但是我遇到了 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.手册对“WantReadError”的描述非常含糊,说它几乎可以来自任何地方。 What am I doing wrong?我究竟做错了什么?

Sometimes in order to send application bytes of an SSL connection, you need to be able to read more bytes from the connection first.有时为了发送 SSL 连接的应用程序字节,您需要首先能够从连接中读取更多字节。 WantReadError is how this case is indicated. WantReadError是这种情况的指示方式。 The only thing you're doing wrong is that you're not handling the WantReadError and then waiting until select indicates that the socket is readable before you try calling send again.您做错的唯一一件事是您没有处理WantReadError然后等到select指示套接字可读,然后再尝试再次调用send

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM