【问题标题】:Capture all live HTTP header data捕获所有实时 HTTP 标头数据
【发布时间】:2015-09-28 14:59:05
【问题描述】:

我没有看到使用 Requests 模块可以做到这一点,但也许我遗漏了一些东西......

我需要能够捕获所有实时 HTTP 标头数据,例如,一个 Firefox 插件,例如创造性地命名为 Live HTTP Headers plugin

是否有捕获头数据的方法,以便我可以收集以下(或尽可能接近以下)?:

https://instagram.com/oauth/authorize/?client_id=cb0096f08a3848e6a355f&redirect_uri=https://pythondev.geometryfletch.com/instagramredirect.html&response_type=code&hl=hu

GET /oauth/authorize/?client_id=cb0096f08a3848e6a355f&redirect_uri=https://pythondev.geometryfletch.com/instagramredirect.html&response_type=code&hl=hu HTTP/1.1
Host: instagram.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: csrftoken=4d9696d270a1d2d7b4d1b5; mid=U8lMswAEAAGyEMGTjENK; __utma=227057989.1190820776.1417498356.1417498356.1417498356.1; sessionid=IGSCb5786690876faa5d2505e1d8b3782691614164cb344c52ec2a6714cb5e1cd884%3Akds8RALygAnGbeQMAiLU%3A%7B%22_token_ver%22%3A1%2C%22_auth_user_id%22%3A324232C%22_token%22%3A9437%3A1lhXdDvRNvbT4MS1J5QpeBmG%3Ac0ccc4aebd1d88175db75c9ce360ad595c55946577bcb9ebc%22%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22last_refreshed%22%3A1436481638.349811%2C%22_platform%22%3A4%7D; ds_user_id=324239437
Connection: keep-alive

HTTP/1.1 302 FOUND
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Language: hu
Content-Type: text/html; charset=utf-8
Date: Thu, 09 Jul 2015 22:46:21 GMT
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Location: https://pythondev.devtesting.com/instagramredirect.html?code=2c49fd7803384c6c5a89cee
Pragma: no-cache
Set-Cookie: csrftoken=4d9696dac6b0d5b8591b5; expires=Thu, 07-Jul-2016 22:46:21 GMT; Max-Age=31449600; Path=/
Vary: Cookie, Accept-Language
Content-Length: 0
Connection: keep-alive

我真正需要的是Location 的 URL 字符串值,看起来像这样:

Location: https://pythondev.devtesting.com/instagramredirect.html?code=2c49fd7803384c6c5a89cee

在四处寻找可能的解决方案后,我一直在尝试以下变体(为这篇文章更改了 client_id 和重定向):

OAuthURL = "https://api.instagram.com/oauth/authorize/?client_id=cb00962b4601317355f&redirect_uri=https://pythondev.instadev.com/instagramredirect.html&response_type=code"
r = requests.get(OAuthURL, stream=True)
print r.raw.data

但显然,我得到了这些乱码:

ôr˼ÖtÉxlÏß5g·Ì{þµ¼æ®6×MƦ¶Ök:µ#î^Bm,\ûf+ÈÕúµçoO´Úö3ut×]Ta¡*_@[BsÊqgÅëêw×ûQÁç)óf-ÕD[³Û®3×*ï¥Ôï`æ:$nÑÞZ£ô)©ª[}«ØBA"¿²å¿*ÜÞ1BuĹ!DGwËUhµ?:PnmwbâÿK¯ÈIÅ¡#2R¸@¼'ø>"dPtOÈm"W fÞ xöñ­¯vmG cÆÔ>÷οaâykãyk¤=²"ù*A¦=ýz=²3&¤ö©½õ CËIMÛÓ¯6Î(íirG*«

Sockets 会为此工作吗?或者是否有另一个模块可以让我以与 Web 浏览器 HTTP 标头插件相同的方式收集标头?

【问题讨论】:

    标签: python http-headers python-requests


    【解决方案1】:

    requests 为您返回标题。您可以使用dict 样式的访问权限来获取它们。

    如果您希望request.get 返回重定向响应而不是自动跟随它,请指定allow_redirects=False

    #UNTESTED
    OAuthURL = "https://api.instagram.com/oauth/authorize/?client_id=cb00962b4601317355f&redirect_uri=https://pythondev.instadev.com/instagramredirect.html&response_type=code"
    r = requests.get(OAuthURL, stream=True, allow_redirects=False)
    print r.headers.keys()
    print r.headers['location']
    

    或者,指定allow_redirects=True(默认值)并检查r.history

    #UNTESTED
    OAuthURL = "https://api.instagram.com/oauth/authorize/?client_id=cb00962b4601317355f&redirect_uri=https://pythondev.instadev.com/instagramredirect.html&response_type=code"
    r = requests.get(OAuthURL, stream=True, allow_redirects=True)
    print [resp.headers['location'] for resp in r.history]
    

    【讨论】:

    • 尝试了您的建议,但仍然收到此乱码回复:ô#Uÿå\msÚHþî_¡h×"d±1R¹Ä©JÕímjÔÞU£©ÆfÍú¿_ÏHáI|ë|UÒ¼t·éyºehÆO^ÿöêÃÞ9±JéÞøç}¢(çísòyê×X÷: aÌuH9q÷.$õ(oÃâíÄa\y _, ô(sÜ©3~ò
    • 那么你没有打印r.headers.keys()。看起来您仍在打印 r.raw.data。
    • 啊,好的,解决了,谢谢。现在收到此错误:print r.headers['location'] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/structures.py", line 54, in __getitem__ return self._store[key.lower()][1] KeyError: 'location'
    • 这意味着服务器没有返回位置标头。您可以检查键列表以查看它确实返回了哪些标题。
    • 好的,所以即使浏览器上的 HTTP 标头插件在响应中显示我正在寻找的重定向 URL 作为 location 的值,Requests 也没有找到它?
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 2015-06-18
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2018-07-14
    • 1970-01-01
    相关资源
    最近更新 更多