有问题的网址是一个纯文本文件,发送时没有任何标题。
请求库似乎坚持至少存在一个头(当您收到索引错误时,它会执行headers[-1]
)。
我不知道如果你能做到在任何请求解决它,但你可以使用urllib
:
import urllib
txt = urllib.urlopen('http://astat.bugly.qq.com').read()
更新:
my goal is to read certain headers from a website, how would you suggest I do that considering the situation ?
服务器发送以下:
~$ telnet astat.bugly.qq.com 80
Trying 103.7.30.121...
Connected to astat-bugly-tgw4l.mig.tencent-cloud.net.
Escape character is '^]'.
GET/HTTP/1.1
HTTP/1.1 200 OK
Date: Thu, 30 Aug 2012 12:01:27 GMT
Server: Apache/2.2.17 (Unix) PHP/5.3.6
Cache-Control: max-age=1800
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/plain; charset=UTF-8
Copyright @ 1998 - 2012 Tencent. All Rights Reserved.
Content-Length: 17
lb monitor page
Connection closed by foreign host.
缺少使用tel净(或类似的低水平),我不认为你会有很多运气得到这些“标题”(当然不应该缩进,或嵌入随机版权字符串)。
无法复制您的问题 - 您安装了什么版本的“requests”? – AChampion
我可以用Python 2.7.13复制它,在win10上请求2.18.3。 – thebjorn
..以及请求2.18.4。 – thebjorn