(感谢您对此的关注!)
我正在尝试使用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 Fri Mar 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
这是挂起的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")
答案 0 :(得分:1)
作为python新手,我忽略了在http.request调用中命名headers参数。它应该显示为:
r = http.request("GET", "https://login.morganstanleyclientserv.com", headers=headers)
感谢Edeki!