小编sti*_*anj的帖子

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

首先,这是针对 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, …
Run Code Online (Sandbox Code Playgroud)

python ssl urllib2 digest-authentication python-3.x

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

digest-authentication ×1

python ×1

python-3.x ×1

ssl ×1

urllib2 ×1