捕获所有实时HTTP标头数据 [英] Capture all live HTTP header data

查看:287
本文介绍了捕获所有实时HTTP标头数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到使用Requests模块这是可能的,但也许我错过了一些东西......



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



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

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

GET /oauth/authorize/?client_id=cb0096f08a3848e6a355f&redirect_uri=https://pythondev.geometryfletch.com/instagramredirect.html&response_type=代码& amp; hl = hu HTTP / 1.1
主机:instagram.com
用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10.9; rv:38.0)Gecko / 20100 101 Firefox / 38.0
接受:text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8
Accept-Language:en-US,en; q = 0.5
接受编码: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
连接:keep-alive

HTTP / 1.1 302 FOUND
缓存控制:私有,无缓存,无存储,必须重新验证
内容 - 语言:hu
内容类型:text / html; charset = utf-8
日期:星期四,09七月2015 22:46:21 GMT
到期日:2000年1月1日星期六00:00:00 GMT
地点:https:// pythondev .devtesting.com / instagramredirect.html?code = 2c49fd7803384c6c5a89cee
Pragma:no-cache
Set-Cookie:csrftoken = 4d9696dac6b0d5b8591b5; expires =星期四,07-Jul-2016 22:46:21 GMT;最大年龄= 31449600; Path = /
变化:Cookie,Accept-Language
内容长度:0
连接:keep-alive

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

 位置:https:/ / / / / / $ pre> 

在搜索可能的解决方案后,我一直在尝试以下变体(此帖子的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:μ#I ^家蚕,\ûf+ÈÕúμçoO'Úö3ut×]的Ta * _ @ [BsÊqgÅëêw×ûQÁç)的-OD [³Û®3×* I¥Ôï`æ:$nÑÞZ£O)©ª[}«大羽¿²å¿*ÜÞ1BuĹDGwËUhμ!?PnmwbâÿK¯ÈIÅ¡#2R ¸@¼'ø>dPtOÈmWfÞxññvmGcÆÔ>÷οaâykãyk¤=²ù* A | =ýz=²3&¤ö©½õCËIMÛÓ¯6Î(íirG*«

套接字为此工作?或者是否有另一个模块,我可以使用,这将允许我收集标题与Web浏览器HTTP标头插件相同的方式?

解决方案

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



如果您想要请求。获取以返回重定向响应而不是自动跟随它,指定 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,全部ow_redirects = True)
print [resp.headers ['location'] for rp in r.history] ​​


I'm not seeing that this is possible using Requests module, but maybe I'm missing something...

I need to be able to capture all the live HTTP header data that, for example, a Firefox plugin such as the creatively named Live HTTP Headers plugin

Is there a method for capturing header data so that I can collect the following (or as close to the following as possible)?:

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

All I really need out of this is the URL string value for Location that look likes this:

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

After searching around for possible solutions, I've been trying variations on the following (client_id and redirect altered for this post):

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

But obviously, I get this garbled stuff:

ô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*«

Would Sockets work for this? Or is there another module out there I could use that would allow me to collect header the same way a web browser HTTP header plugin would?

解决方案

requests returns the headers for you. You can use dict-style access to get them.

If you want request.get to return the redirect response rather than automatically following it, specify 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']

Alternatively, specify allow_redirects=True (the default value), and examine 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]

这篇关于捕获所有实时HTTP标头数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆