在urllib3中,HTTP请求挂起...但不是卷曲

问题描述 投票:2回答:1

(谢谢你看看这个!)

我正在尝试使用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没有问题。

我们是 - 那; Peon 3 -v返回:

Linux ubuntu 4.18.0-17-generic#18~18.04.1-Ubuntu SMP Fri 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")

python-3.x https urllib3
1个回答
1
投票

作为一个python新手,我忽略了在http.request调用中命名headers参数。它本应该读:

r = http.request("GET", "https://login.morganstanleyclientserv.com", headers=headers)

感谢Edeki!

© www.soinside.com 2019 - 2024. All rights reserved.