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

sti*_*anj 6 python ssl urllib2 digest-authentication python-3.x

首先,这是针对 Gira Homeserver 的,它是家庭自动化服务器。它有Python 2.7,我无法安装外部模块。

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

我想做的是从我的飞利浦 Android TV 的网络服务器读取内容。这适用于浏览器,适用于 Curl,也适用于 Python 请求。但它不适用于 urllib2,而我需要使用 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)
Run Code Online (Sandbox Code Playgroud)

请求示例:

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)
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

我知道现在总是建议使用请求,但这对我来说不是一个选择。有人能解释一下握手错误吗?

这几天我一直在为这个问题苦恼……

小智 5

我不熟悉你正在执行的任务,但由于网络不好而遇到了这样的错误。我在使用 youtube-dl 下载视频时遇到了同样的错误,但只需切换到另一个互联网即可解决该问题。此外,由于网络状况不佳,SSH 隧道也无法工作。