【问题标题】:Unable to connect to the Simple HTTPS Server无法连接到简单 HTTPS 服务器
【发布时间】:2016-04-15 01:18:44
【问题描述】:

我从以下位置复制了代码:

https://www.piware.de/2011/01/creating-an-https-server-in-python/

并创建了新的 pem 文件,如下所示:

sh-3.2# openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 10
Generating a 2048 bit RSA private key
.......................+++
...............................+++
writing new private key to 'key.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:US
State or Province Name (full name) [Berkshire]:CA
Locality Name (eg, city) [Newbury]:CA
Organization Name (eg, company) [My Company Ltd]:Test
Organizational Unit Name (eg, section) []:Test
Common Name (eg, your name or your server's hostname) []:mybox.com
Email Address []:me@test.com

它创建了 2 个文件 cert.pem 和 key.pem。所以我的最终代码是:

import BaseHTTPServer, SimpleHTTPServer
import ssl

httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='/myhome/cert.pem', server_side=True)
httpd.serve_forever()

然后我运行了我的程序:

python myserver.py

但是当我尝试从浏览器访问它时:

https://mybox.com:4443 

我无法建立连接,但是当我尝试如下时:

python -m SimpleHTTPServer 4443 

然后尝试通过浏览器访问我得到以下错误:

An error occurred during a connection to mybox.com:4443. SSL received a record that exceeded the maximum permissible length. Error code: SSL_ERROR_RX_RECORD_TOO_LONG

我的目标是制作一个简单的 HTTPS 服务器。请让我知道如何解决此问题? =======================更新========================= =======

我将 key.pem 文件复制到 cert.pem

cat key.pem >> cert.pem

现在当我启动我的服务器时:

python ./try.py

点击网址

https://mybox.com:15368/

我看到浏览器状态“已连接到 mybox.com:4443”,但一直在等待响应页面。在框中,我看到以下输出:

# python try.py
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:
Enter PEM pass phrase:

我需要继续输入我在创建 cert 和 pem 文件时使用的相同密码

【问题讨论】:

    标签: python python-2.x


    【解决方案1】:

    我假设您在 hosts 文件中设置了 mybox.com,但您需要包含端口。 HTTPS 默认尝试访问 443。您需要指定端口

    HTTPS: //mybox.com:4443

    【讨论】:

    • 是的,它在 /etc/hosts 文件中并以相同的方式访问 mybox.com:4443 - 更新了我的问题
    • 如果您包含“.”,则为您的证书文件位置对于当前目录还是实际上在根目录中? certfile='./myhome/cert.pem'
    • 路径是正确的,只是我使用了绝对路径和相对路径
    • 如果您在 openssl 命令中包含 -nodes 来创建密钥,它是否仍然要求输入密码? --从您链接到 openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes 的页面上的 cmets
    【解决方案2】:

    请将您的最后第二行更改如下 - 添加密钥文件并重试。它适用于我(ubuntu,python2.7)

    httpd.socket = ssl.wrap_socket (httpd.socket, certfile='cert.pem', server_side=True, **keyfile="key.pem"**)
    

    【讨论】:

    • 文件“try.py”,第 6 行 httpd.socket = ssl.wrap_socket (httpd.socket, certfile='cert.pem', server_side=True, keyfile="key.pem ") ^ SyntaxError:Python 2.7.3 语法无效(默认,2013 年 2 月 7 日,12:12:45)
    • 嗨,我的 python 版本是 Python 2.7.6(默认,2015 年 6 月 22 日,17:58:13)
    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2013-10-28
    • 2017-12-20
    • 1970-01-01
    • 2012-03-24
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多