请求同一个网址, requests 和 urllib2 返回的 headers 内容不同? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dofine
V2EX    Python

请求同一个网址, requests 和 urllib2 返回的 headers 内容不同?

  •  
  •   dofine 2017-01-14 17:12:10 +08:00 4019 次点击
    这是一个创建于 3258 天前的主题,其中的信息可能已经有所发展或是发生改变。

    urllib2:

    Date: XXX Server: Apache Last-Modified: XXX Accept-Ranges: bytes Content-Length: 12345678 Vary: Accept-Encoding Connection: close Content-Type: text/plain 

    requests:

    Content-Encoding: gzip Accept-Ranges: bytes Vary: Accept-Encoding Keep-alive: timeout=5, max=128 Last-Modified: XXX Connection: Keep-Alive ETag: xxxxxxxxx Content-Type: text/plain 

    为何 requests 少了 content-length ?其它发送请求的设置是完全一样的。。 requests 和 Chrome 开发者工具查看到的一致。但是这里我又需要 content-length 的值(为了断点续传)

    第 1 条附言    2017-01-14 17:56:30 +08:00
    import urllib2 import requests url = 'exmaple.com' headers = { "Authorization": "Basic xxxx", "Range": "bytes=0-" } req = urllib2.Request(url, headers=headers) resp = urllib2.urlopen(req) print resp.info() r = requests.get(url, headers=headers) print r.headers assert resp.info()['ETag'] == r.headers['ETag'] 
    Date: Sat, 14 Jan 2017 09:39:50 GMT Server: Apache Last-Modified: Sat, 14 Jan 2017 09:39:49 GMT ETag: "e91103-10e04f7-5460abb4743a3" Accept-Ranges: bytes Content-Length: 17695991 Vary: Accept-Encoding Content-Range: bytes 0-17695990/17695991 Connection: close Content-Type: text/plain {'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Accept-Ranges': 'bytes', 'Vary': 'Accept-Encoding', 'Keep-Alive': 'timeout=5, max=128', 'Server': 'Apache', 'Last-Modified': 'Sat, 14 Jan 2017 09:39:49 GMT', 'Connection': 'Keep-Alive', 'ETag': '"e91103-10e04f7-5460abb4743a3"', 'Date': 'Sat, 14 Jan 2017 09:39:50 GMT', 'Content-Type': 'text/plain'} 

    我也知道肯定是两次发送的请求header不一样。。。现在总算解决了。。

    第 2 条附言    2017-01-14 17:57:18 +08:00

    The response is different because requests indicates that it supports gzip-encoded bodies, by sending an Accept-Encoding: gzip, deflate header field. urllib2 does not. You'll find if you added that header to your urllib2 request that you get the new behaviour.

    Clearly, in this case, the server is dynamically gzipping the responses. This means it doesn't know how long the response will be, so it is sending using chunked transfer encoding.

    If you really must get the Content-Length header, then you should add the following headers to your Requests request: {'Accept-Encoding': 'identity'}.

    15 条回复    2017-01-14 23:11:08 +08:00
    redhatping
        1
    redhatping  
       2017-01-14 17:18:47 +08:00
    看官方文档
    binux
        2
    binux  
       2017-01-14 17:20:43 +08:00 via Android
    Content-Length 不应该手动设置
    dofine
        3
    dofine  
    OP
       2017-01-14 17:24:10 +08:00
    @binux 我描述不清~~上边给出的结果是响应的 header ,我的意思是需要知道当前 content-length 的值。。但是 requests 的返回里面没有。。 urllib2 就有。。
    @redhatping 文档已经看了许多遍了。。怀疑是服务器的问题?
    hahastudio
        4
    hahastudio  
       2017-01-14 17:25:47 +08:00
    你这是结果,肯定还是因为你发送的请求不一样
    Lonely
        5
    Lonely  
       2017-01-14 17:36:14 +08:00 via iPhone
    你把代码也贴出来啊……
    dofine
        6
    dofine  
    OP
       2017-01-14 17:37:59 +08:00
    {'Range': 'bytes=0-', 'Authorization': 'Basic XXX'}

    手动加了这个 header , urllib2 和 requests 返回的 ETag 都是一样的啊。。为什么会发送请求不一样呢。。 @hahastudio
    dofine
        7
    dofine  
    OP
       2017-01-14 17:40:06 +08:00
    ```
    import os
    import urllib2
    import requests

    url = 'exmaple.com'
    headers = {
    "Authorization": "Basic xxxx",
    "Range": "bytes=0-"
    }
    req = urllib2.Request(url, headers=headers)
    resp = urllib2.urlopen(req)
    print resp.info()

    r = requests.get(url, headers=headers)
    print r.headers
    assert resp.info()['ETag'] == r.headers['ETag']
    ```
    dofine
        8
    dofine  
    OP
       2017-01-14 17:42:28 +08:00
    @Lonely 贴在楼上了。。我发现我都不在回复里贴代码了。。
    lhbc
        9
    lhbc  
       2017-01-14 17:45:03 +08:00   1
    明显你的 request header 不一样
    hahastudio
        10
    hahastudio  
       2017-01-14 17:45:36 +08:00   1
    dofine
        11
    dofine  
    OP
       2017-01-14 17:51:02 +08:00
    @hahastudio 开始就使用的文档里的方法,结果跟换成手动设置 auth 一样的。。
    lbp0200
        12
    lbp0200  
       2017-01-14 17:55:21 +08:00   1
    试试随机 ua
    dofine
        13
    dofine  
    OP
       2017-01-14 17:57:34 +08:00
    谢谢大家。。
    dsg001
        14
    dsg001  
       2017-01-14 19:10:06 +08:00
    抓包看看发送的请求有木有区别
    qgy18
        15
    qgy18  
       2017-01-14 23:11:08 +08:00 via iPhone   1
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1326 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 17:02 PVG 01:02 LAX 09:02 JFK 12:02
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86