python请求相当于curl -H [英] python requests equivalent to curl -H

查看:556
本文介绍了python请求相当于curl -H的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想订阅来自我的粒子光子的事件流。
文档建议

  curl -H授权:承载{ACCESS_TOKEN_GOES_HERE}\ 
https://api.particle.io/v1/events/motion-detected

我试过了

  address3 ='https://api.particle.io/v1/events/motion-detected'
data = {'access_token':access_token}
r3 = requests.get address3,params = data)

但我没有任何东西, >

我希望得到如下响应:

 事件:motion-detected 
data:{data:intact,ttl:60,published_at:2015-06-25T05:08:22.136Z,coreid:coreid}

event:motion-detected
data:{data:broken,ttl:60,published_at:2015-06-25T05:08:23.014Z, :coreid}



我只是不明白curl是做什么相对于什么请求做。
感谢您的帮助,
JR

解决方案

自定义标头作为头文件中的字典传递参数

  address3 ='https://api.particle.io/v1/events/motion-detected'
data = {'Authorization':'Bearer {ACCESS_TOKEN_GOES_HERE}'}
r3 = requests.get(address3,headers = data)

params 参数用于传递网址参数。基本上你的代码向 https://api.particle.io/v1/events/motion-detected?access_token=token_goes_here 发出请求,这可以通过打印url print(r3.url)


I'm trying to subscribe to an event stream coming from my particle photon. The docs suggest

curl -H "Authorization: Bearer {ACCESS_TOKEN_GOES_HERE}" \
https://api.particle.io/v1/events/motion-detected

I've tried

address3 ='https://api.particle.io/v1/events/motion-detected'
data = {'access_token': access_token}
r3 = requests.get(address3,params=data)

but I get nothing, and I mean nothing, in response

I expect a response like:

event: motion-detected
data: {"data":"intact","ttl":"60","published_at":"2015-06-25T05:08:22.136Z","coreid":"coreid"}

event: motion-detected
data: {"data":"broken","ttl":"60","published_at":"2015-06-25T05:08:23.014Z","coreid":"coreid"}

I just don't understand what curl is doing relative to what requests is doing. Thanks for the help, JR

解决方案

Custom headers are passed as a dictionary in headers argument

address3 ='https://api.particle.io/v1/events/motion-detected'
data = {'Authorization': 'Bearer {ACCESS_TOKEN_GOES_HERE}'}
r3 = requests.get(address3, headers=data)

params argument is used to pass URL parameters. Basically your code issues a request to https://api.particle.io/v1/events/motion-detected?access_token=token_goes_here, this can be veriefied by printing url print(r3.url)

这篇关于python请求相当于curl -H的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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