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

时间:2019-07-10 21:43:13

标签: python urllib2

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

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

我想做的是从Philips 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超时。

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

几天来我一直在撞我的头...

1 个答案:

答案 0 :(得分:1)

我不熟悉您正在执行的任务,但是由于网络错误而遇到了此类错误。使用youtube-dl下载视频时,我遇到了相同的错误,但仅通过切换到另一个Internet即可解决。此外,由于网络故障,SSH隧道无法正常工作。