【问题标题】:WSGIServer not working with https and python3WSGIServer 不适用于 https 和 python3
【发布时间】:2021-11-29 19:17:34
【问题描述】:

我们有一个用 python2 编写的旧包,并正在努力将其升级到 python3。这是一个网络应用程序,我们正在使用 WSGIServer。

from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler

server = pywsgi.WSGIServer(('', port), apphandler, handler_class=WebSocketHandler)

def apphandler(request, start_response):
    log.info("ATTENTION: request is {}".format(request))
    # do something

但是,当我尝试通过 https://website 使用网络应用程序时,它会显示此错误:

This site can’t provide a secure connection
website.com sent an invalid response.
ERR_SSL_PROTOCOL_ERROR

在我的服务器日志中,我会看到如下内容:

Invalid HTTP method: '\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03Èæ\x01\x92É\x16ÁW»P½\x1aBÐa\x83ÆÊa]ãíDp¥¥¥\x12Ç\x82|\x1f E5\x03aAv\x99¢Nª\x93ÅÏ:Ð\x9d÷3£\x80çÍïÌÑÿC\rÏUÄ\x8b\x00 ªª\x13\x01\x13\x02\x13\x03À+À/À,À0̨̩À\x13À\x14\x00\x9c\x00\x9d\x00/\x005\x01\x00\x01\x93ZZ\x00\x00\x00\x00\x005\x003\x00\x000website.com\x00\x17\x00\x00ÿ\x01\x00\x01\x00\x00\n'

当我实际将网站用作http://website.com 时,它会起作用。而在python2中,服务器会正​​确响应https。

我猜这可能是编码问题,但它甚至不会到达我在 apphandler 函数中放置日志的位置。有谁知道我该如何解决这个问题?我需要更改服务器或 python3 编码设置吗?

【问题讨论】:

    标签: python-3.x wsgi gevent


    【解决方案1】:

    看起来您正在启动一个位于端口 443 上的普通 TCP Web 服务器,然后使用 https: url 从 Web 浏览器访问它。

    如果您查看文档:

    https://www.gevent.org/api/gevent.pywsgi.html#gevent.pywsgi.WSGIServer

    您需要提供ssl_args 以使套接字在 SSL 而不是 TCP 下运行。仅提供 443 端口号是不够的。

    您可以在此处找到一些如何设置 ssl_args 的示例:

    https://www.programcreek.com/python/example/78007/gevent.pywsgi.WSGIServer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-08
      • 2018-01-21
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 2013-09-24
      相关资源
      最近更新 更多