python - 无法连接到简单 HTTPS 服务器

标签 python python-2.x

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

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

并点击 URL

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:

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

最佳答案

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

HTTPS://mybox.com:4443

关于python - 无法连接到简单 HTTPS 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36636715/

相关文章:

python - 避免在 python 中为 Long 加上 L 后缀

python - 如何避免将循环索引泄漏到 python 2.x 的命名空间中?

c# - 在 C# 中使用 foreach 循环时修改集合

python - 使用内存缓冲区的 psycopg2 Postgres COPY EXPERT 到 Pandas read_csv 失败并出现 ValueError

python - Django 每个用户 View 缓存

python - 企业防火墙背后的网络流量

Python、__getitem__、切片和负索引

python - Python将函数闭包的名称绑定(bind)存放在哪里?

python - 导入 scipy.stats 时出现 "from new import instancemethod"后错误

python - Pandas Dataframe 检查列值是否在列列表中