我从下面的位置复制了代码:
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文件时使用的相同密码短语
答案 0 :(得分:0)
我假设您在主机文件中设置了mybox.com,但您需要包含该端口。 HTTPS默认尝试访问443。您需要指定端口
HTTPS://mybox.com:4443
答案 1 :(得分:-1)
请更改您的最后一行,如下所示 - 添加密钥文件,然后重试。它适用于我(ubuntu,python2.7)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='cert.pem', server_side=True, **keyfile="key.pem"**)