pyOpenSSL 和 WantReadError

发布于 2024-08-24 09:37:11 字数 1150 浏览 12 评论 0原文

我有一个套接字服务器,我试图将其转移到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2024-08-31 09:37:11

有时,为了发送 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 the WantReadError and then waiting until select indicates that the socket is readable before you try calling send again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文