Urllib2 HTTPS截断响应

sle*_*anc 1 python https urllib2 urlopen

我正在尝试使用urllib2.urlopen(实际上,我正在使用mechanize,但这是mechanize调用的方法)获取页面当我获取页面时,我得到的回复不完整; 页面被截断.但是,如果我访问页面的非HTTPS版本,我将获得完整的页面.

我在Arch Linux(3.5.4-1-ARCH x86_64)上.我运行openssl 1.0.1c.在我拥有的另一台Arch Linux机器上会出现此问题,但在使用Python 3(3.3.0)时则不会.

这个问题似乎与urllib2没有检索整个HTTP响应有关.

我在唯一允许我使用urllib2(Py I/O)的在线Python解释器上测试它,它按预期工作.这是代码:

import urllib2

u = urllib2.urlopen('https://wa151.avayalive.com/WAAdminPanel/login.aspx?ReturnUrl=%2fWAAdminPanel%2fprivate%2fHome.aspx')

print u.read()[-100:]
Run Code Online (Sandbox Code Playgroud)

最后一行应包含通常的内容</body></html>.

当我试穿urllib.urlretrieve我的机器时,我得到:

ContentTooShortError: retrieval incomplete: got only 11365 out of 13805 bytes
Run Code Online (Sandbox Code Playgroud)

我无法测试urlretrieve在线解释器,因为它不会让用户写入临时文件.晚上,我将尝试从我的机​​器上获取URL,但是从不同的位置.

Bal*_*rol 5

我在不同的Linux系统上使用Python 2.7得到了同样的错误:

>>> urllib.urlretrieve('https://wa151.avayalive.com/WAAdminPanel/login.aspx?ReturnUrl=%2fWAAdminPanel%2fprivate%2fHome.aspx')
---------------------------------------------------------------------------
ContentTooShortError                      Traceback (most recent call last)
...
ContentTooShortError: retrieval incomplete: got only 11365 out of 13805 bytes
Run Code Online (Sandbox Code Playgroud)

但是,可以使用以下方法完成相同的操作(实际上对我有用)requests:

>>> import requests
>>> r = requests.get('https://wa151.avayalive.com/WAAdminPanel/login.aspx?ReturnUrl=%2fWAAdminPanel%2fprivate%2fHome.aspx')
>>> with open(somefilepath, 'w') as f:
...     f.write(r.text)
Run Code Online (Sandbox Code Playgroud)

这对你有用吗?