使用 urllib 的“握手操作超时”,适用于请求

Posted

技术标签:

【中文标题】使用 urllib 的“握手操作超时”,适用于请求【英文标题】:"The handshake operation timed out" with urllib, works with requests 【发布时间】:2019-07-10 21:43:13 【问题描述】:

首先,这适用于 Gira Homeserver,它是一种家庭自动化服务器。它有 Python 2.7,我无法安装外部模块。

但对于测试和示例,我一直在使用 python 2.7.15 和 python 3.6.8(但也尝试了一些其他版本 - 结果相同)

我要做的是从我的飞利浦 android TV 的网络服务器读取内容。这适用于浏览器,适用于 Curl,适用于 Python 请求。但它不适用于 urllib2,这是我需要使用它才能与我的家庭自动化系统一起使用。

电视在需要摘要式身份验证的 https 网页上提供 json 输出。

Urllib 示例(Python3,能够与请求进行比较):

import urllib.request
import ssl

url="https://192.168.3.100:1926/6/powerstate"
username="6AJeu5Ffdm9dQnum"
password="5a21386d952c2f1fbe66be2471d98c391bb918a6d2130cdf1b6deb2b87872eaa"

ctx = ssl._create_unverified_context()

auth = urllib.request.HTTPDigestAuthHandler(urllib.request.HTTPPasswordMgrWithDefaultRealm())
auth.add_password (None, url, username, password)

opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx,debuglevel=1), auth)
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url,None,10)

请求示例:

import requests
import ssl

url="https://192.168.3.100:1926/6/powerstate"
username="6AJeu5Ffdm9dQnum"
password="5a21386d952c2f1fbe66be2471d98c391bb918a6d2130cdf1b6deb2b87872eaa"


from requests.auth import HTTPDigestAuth
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
r = requests.get(url, auth=HTTPDigestAuth(username, password), timeout=10, verify=False)
print(r.status_code)
print(r.content)

urllib 示例超时并出现以下错误:

stianj@buick:~$ python3 stack.py
send: b'GET /6/powerstate HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: 192.168.3.100:1926\r\nUser-Agent: Python-urllib/3.6\r\nConnection: close\r\n\r\n'
reply: 'HTTP/1.1 401 Unauthorized\r\n'
header: Date: Wed, 10 Jul 2019 21:36:45 GMT+00:00
header: Accept-Ranges: bytes
header: Server: Restlet-Framework/2.3.12
header: WWW-Authenticate: Digest realm="XTV", domain="/", nonce="MTU2Mjc5NDYwNTI3ODo1NTNlMTFhYzk5MjJjODQyMTYyZjAxZjRhYmYyYzNhMA==", algorithm=MD5, qop="auth"
header: Content-Length: 424
header: Content-Type: text/html; charset=UTF-8
Traceback (most recent call last):
  File "/usr/lib/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/usr/lib/python3.6/http/client.py", line 1400, in connect
    server_hostname=server_hostname)
  File "/usr/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/usr/lib/python3.6/ssl.py", line 817, in __init__
    self.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
socket.timeout: _ssl.c:835: The handshake operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "stack.py", line 15, in <module>
    response = urllib.request.urlopen(url,None,10)
  File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.6/urllib/request.py", line 564, in error
    result = self._call_chain(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 1208, in http_error_401
    host, req, headers)
  File "/usr/lib/python3.6/urllib/request.py", line 1089, in http_error_auth_reqed
    return self.retry_http_digest_auth(req, authreq)
  File "/usr/lib/python3.6/urllib/request.py", line 1103, in retry_http_digest_auth
    resp = self.parent.open(req, timeout=req.timeout)
  File "/usr/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/usr/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.6/urllib/request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.6/urllib/request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error _ssl.c:835: The handshake operation timed out>

虽然请求示例工作得很好(显示网页的输出。由于我对两个示例使用相同的 Python 版本,我希望 ssl 参数和密码等是相同的。什么是 非常 有趣的是,POST 与 urllib 一起工作得很好。只是 GET 超时。

我知道现在总是建议使用 requests,但这不是我的选择。有人能解释一下握手错误吗?

这几天一直在敲我的头......

【问题讨论】:

或者有没有办法获取更多日志以找出失败的原因?非常有趣的是,仅添加后数据就可以使握手工作。但这不会给我我想要的数据:) 【参考方案1】:

我不熟悉您正在处理的任务,但是由于网络不佳而遇到此类错误。我在使用 youtube-dl 下载视频时遇到了同样的错误,但只需切换到另一个互联网即可解决。由于网络不好,SSH 隧道也不起作用。

【讨论】:

遇到类似问题,这个答案鼓励我简单地关闭/打开我的 WiFI,这就解决了问题。

以上是关于使用 urllib 的“握手操作超时”,适用于请求的主要内容,如果未能解决你的问题,请参考以下文章

urlliburllib2urllib3区别和使用

Urllib 库使用

python使用requests模块完成get/post/代理/自定义header/自定义cookies

urllib2.HTTPError:HTTP 错误 403:禁止

获取请求适用于邮递员,但适用于浏览器

用于关闭 impala 查询的 Python 脚本。我正在使用 urllib 和 json 库