【问题标题】:What is wrong with urllib.request.Request(url=,data)urllib.request.Request(url=,data) 有什么问题
【发布时间】:2011-11-07 15:45:41
【问题描述】:

我在我的电脑(Ubuntu 10.04 和 python 3.2)上测试来自 http://docs.python.org/py3k/library/urllib.request.html 的代码

>>> import urllib.request
>>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
... data=b'This data is passed to stdin of the CGI')
>>> f = urllib.request.urlopen(req)
>>> print(f.read().decode('utf-8'))
Got Data: "This data is passed to stdin of the CGI"

使用服务器

python3 -m http.server

urlopen(req)时得到如下消息:

......
ssl.SSLError: [Errno 1] _ssl.c:390: error:140770FC:SSLroutines:SSL23_GET_SERVER_HELLO:unknown protocol

During handling of the above exception, another exception occurred:
......
urllib.error.URLError: <urlopen error [Errno 1] _ssl.c:390: error:140770FC:SSL     routines:SSL23_GET_SERVER_HELLO:unknown protocol>

有什么建议吗?非常感谢。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    SSL 将加密添加到通过 HTTP 进行的基本通信。这意味着除非您使用 SSL 库,否则正常连接会将其视为杂乱数据。

    【讨论】:

    • 感谢您的回答。现在我的代码为:url='localhost:8000/cgi-bin/test.cgi'; handler=urllib.request.HTTPSHandler(url); opener=urllib.request.build_opener(handler); f = opener.open(url,data=b'这个数据被传递给CGI的stdin')。但是错误信息为:“urllib.error.HTTPError: HTTP Error 501: Unsupported method ('POST')”
    • 对不起,我现在知道了,这是我的错。
    • 那么,答案是什么?您可以在此帖子上将其作为单独的答案发布。我来这里寻找解决方案,但找不到任何解决方案。
    • @joar 您必须在开启程序中添加一个变量,在本例中称为处理程序。之前,您使用 urllib.request.HTTPSHandler(url) 创建了处理程序,而不是简单地使用 urllib.request(url)。
    • 但无论如何我都没有看到 httpshandler...在文档docs.python.org/3.0/library/urllib.request.html 上。
    猜你喜欢
    • 2021-01-05
    • 2014-11-04
    • 2015-09-14
    • 1970-01-01
    • 2012-06-14
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多