Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python请求和urllib2在连接到同一主机时获得不同的头_Python_Python Requests_Urllib2 - Fatal编程技术网

Python请求和urllib2在连接到同一主机时获得不同的头

Python请求和urllib2在连接到同一主机时获得不同的头,python,python-requests,urllib2,Python,Python Requests,Urllib2,我们有一个服务器提供.txt文件,基本上一些日志文件会随着时间的推移而增长。当我使用urlib2向服务器发送GET时r=urlib2.urlopen('http://example.com”),响应的标题将是: Date: XXX Server: Apache Last-Modified: XXX Accept-Ranges: bytes Content-Length: 12345678 Vary: Accept-Encoding Connection: close Content-Type:

我们有一个服务器提供.txt文件,基本上一些日志文件会随着时间的推移而增长。当我使用
urlib2
向服务器发送
GET
r=urlib2.urlopen('http://example.com”)
,响应的标题将是:

Date: XXX
Server: Apache
Last-Modified: XXX
Accept-Ranges: bytes
Content-Length: 12345678
Vary: Accept-Encoding
Connection: close
Content-Type: text/plain
而if
r=requests.get('http://example.com)

第二个响应与我使用chrome开发工具得到的响应相同。那么,为什么两者不同呢?我需要
内容长度
头来确定每次需要下载多少字节,因为文件可能会变得非常大

编辑: 使用
httpbin.org/get
测试:

urllib2响应:

{u'args': {},
 u'headers': {u'Accept-Encoding': u'identity',
              u'Host': u'httpbin.org',
              u'User-Agent': u'Python-urllib/2.7'},
 u'origin': u'ip',
 u'url': u'http://httpbin.org/get'}
{u'args': {},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate',
              u'Host': u'httpbin.org',
              u'User-Agent': u'python-requests/2.11.1'},
 u'origin': u'ip',
 u'url': u'http://httpbin.org/get'}
响应标题:

Server: nginx
Date: Sat, 14 Jan 2017 07:41:16 GMT
Content-Type: application/json
Content-Length: 207
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Server : nginx
Date : Sat, 14 Jan 2017 07:42:39 GMT
Content-Type : application/json
Content-Length : 239
Connection : keep-alive
Access-Control-Allow-Origin : *
Access-Control-Allow-Credentials : true
请求和答复:

{u'args': {},
 u'headers': {u'Accept-Encoding': u'identity',
              u'Host': u'httpbin.org',
              u'User-Agent': u'Python-urllib/2.7'},
 u'origin': u'ip',
 u'url': u'http://httpbin.org/get'}
{u'args': {},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate',
              u'Host': u'httpbin.org',
              u'User-Agent': u'python-requests/2.11.1'},
 u'origin': u'ip',
 u'url': u'http://httpbin.org/get'}
响应标题:

Server: nginx
Date: Sat, 14 Jan 2017 07:41:16 GMT
Content-Type: application/json
Content-Length: 207
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Server : nginx
Date : Sat, 14 Jan 2017 07:42:39 GMT
Content-Type : application/json
Content-Length : 239
Connection : keep-alive
Access-Control-Allow-Origin : *
Access-Control-Allow-Credentials : true
github的报价:

响应不同,因为请求表明它支持 gzip编码的实体,通过发送一个接受编码:gzip,deflate 标题字段。urllib2没有。如果添加了该标题,您将发现 请求您获得新行为

显然,在这种情况下,服务器正在动态地gzip 响应。这意味着它不知道响应会持续多久, 因此,它是使用分块传输编码发送的

如果确实必须获取内容长度标题,那么应该添加 请求的以下标题:{'Accept-Encoding': “身份”}


是否在每种情况下都发送相同的标头?是否确定要访问的服务完好无损?因为这是一项公司服务,也许有人犯了错误,在同一虚拟ip下几乎没有不同的Web服务器?如果您尝试在chrome中刷新(CTRL+F5),是否始终得到相同的响应?@snakecharmerb您好,是的,服务器需要基本身份验证。在urllib2中,我手动添加“授权”:“基本XXX”,而在请求中,我使用requests.get('url',auth=('user','password'))。所有其他设置都是默认设置。@iTayb In chrom I get 200或304未不时修改。如果日志文件没有更改,状态代码304是否正确?这里是关于http的新手,但服务器端不在我的控制范围之内:)您还可以尝试两件事,否则我不知道:(1)发送一个带有
请求的“Connection:close”头,看看这是否会迫使服务器在响应中提供内容长度;(2) 向服务器发送HEAD请求,查看该请求是否包含内容长度。