python - "The handshake operation timed out"与 urllib,适用于请求

标签 python python-3.x ssl urllib2 digest-authentication

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

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

我想要做的是从我的飞利浦安卓电视的网络服务器读取内容。这适用于浏览器,适用于 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 ,但这对我来说不是一个选择。有人会解释握手错误吗?

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

最佳答案

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

关于python - "The handshake operation timed out"与 urllib,适用于请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56979039/

相关文章:

ssl - 结合 Wireshark 使用 Burp Suite 代理嗅探 https/SSL 流量

python - 抑制绘图子图中的 y 轴标签,注释未对齐

python - 我的背包代码输出错误?

java - 如何避免在 Spring Boot 应用程序中写入显式端口号 (8443)

python - Pandas 日期时间减法 - 分配 nan 值

python - 提示 : No function matches the given name and argument types. 您可能需要添加显式类型转换。 TrigramSimilarity Django

java - HttpClientBuilder 问题 : Could not initialize class sun. security.ssl.SSLContextImpl$CustomizedTLSContext

python - 如何在python中获取该行以及该行之前和该行之后的数据

python - 使用 pip 命令安装软件包 'preprocess' 错误

通过迭代创建字典的 Pythonic 方法