bde*_*hak 5 https urllib3 python-3.x
(感谢您查看此内容!)
我正在尝试使用 python3 和简单的 urllib3 http.request 从https://login.morganstanleyclientserv.com读取 HTML 。
看起来服务器正在重置连接,最终 urllib3 的重试放弃。
这里是否存在 TLS 协商问题?如果是这样,urllib3如何补偿?
或者问题出在其他地方?如何解决这个问题?
我已经尝试使用curl进行相同的(?)事务...它返回预期的HTML,没有任何延迟。
我还尝试从不同的站点阅读(例如,https://client.schwab.com/Login/SignOn/CustomerCenterLogin.aspx)...没问题。
Chrome 加载https://login.morganstanleyclientserv.com没有问题。
uname -a ; python3 -V 返回:
Linux ubuntu 4.18.0-17-generic #18~18.04.1-Ubuntu SMP 3月15日星期五15:27:12 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux Python 3.6.7
这是有效的卷曲:
curl -v --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" --header "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" --header "Accept-Encoding: text/plain" --header "Accept-Language: en-US,en;q=0.9" --output foo https://login.morganstanleyclientserv.com
Run Code Online (Sandbox Code Playgroud)
这是挂起的 python3 + urllib3 代码(打印 1 后,然后打印 2,但不打印其他任何内容):
import urllib3
import certifi
print (1)
try:
http = urllib3.PoolManager(cert_reqs = 'CERT_REQUIRED',
ca_certs = certifi.where())
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'text/plain',
'Accept-Language':'en-US,en;q=0.9'
}
print (2)
# *** This hangs ***
r = http.request("GET", "https://login.morganstanleyclientserv.com", headers)
print (3)
print (r.data)
print (4)
except Exception as e:
print(e)
except:
print("error")
Run Code Online (Sandbox Code Playgroud)
作为一个 python 新手,我忽略了在 http.request 调用中命名 headers 参数。它应该是这样写的:
r = http.request("GET", "https://login.morganstanleyclientserv.com", headers=headers)
Run Code Online (Sandbox Code Playgroud)
感谢埃德基!
| 归档时间: |
|
| 查看次数: |
1335 次 |
| 最近记录: |