简体   繁体   English

卷毛有效,但python请求不起作用

[英]Curl works but python requests doesn't

When I do curl, I get a response: 当我卷曲时,我得到一个响应:

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"}

However, when I do python requests to the same URL I get a status 410. 但是,当我向相同的网址发出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]>>

What gives? 是什么赋予了?

The host is a standard Ubuntu docker image and only installed Curl and some python modules. 主机是标准的Ubuntu docker映像,仅安装了Curl和一些python模块。 Python -V is 2.7 Python -V为2.7

Note: I looked at this question but I can't telnet into above server so that solution doesn't apply to me: Curl works but not Python requests 注意:我看了这个问题,但是我无法远程登录到上述服务器,因此该解决方案不适用于我: Curl有效但Python请求无效

You've made at least two errors in your program. 您在程序中至少犯了两个错误。

1) You haven't specified the data= or headers parameters to the requests.get() call. 1)您尚未为requests.get()调用指定data=headers参数。 Try this: 尝试这个:

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

2) .json is a method, not a data attribute of the response object. 2) .json是方法,而不是响应对象的数据属性。 As a method, it must be called in order to be effective. 作为一种方法,必须先调用它才能有效。 Try this: 尝试这个:

print r.json()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM