Curl有效但python请求不起作用

时间:2018-03-08 03:26:53

标签: python python-2.7 http docker curl

当我卷曲时,我得到了回复:

root@3d7044bac92f:/home/app/tmp# curl -H "Content-type: application/json" -X GET https://github.com/timeline.json -k 

{"message":"Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}

但是,当我对同一个URL发出python请求时,我得到一个状态410。

import requests

headers = {
    'Content-type': 'application/json',
}

r = requests.get('https://github.com/timeline.json')
print r.json

root@3d7044bac92f:/home/app/tmp# python rest.py 
<bound method Response.json of <Response [410]>>

是什么给出的?

主机是标准的Ubuntu docker镜像,只安装了Curl和一些python模块。 Python -V是2.7

注意:我查看了这个问题,但我无法远程登录到上面的服务器,因此该解决方案并不适用于我: Curl works but not Python requests

1 个答案:

答案 0 :(得分:2)

您的程序中至少出现了两个错误。

1)您尚未为data=电话指定headersrequests.get()个参数。试试这个:

 r = requests.get('https://github.com/timeline.json', data=data, headers=headers)

2).json是一个方法,而不是响应对象的数据属性。作为一种方法,必须调用它才能有效。试试这个:

print r.json()