卷毛有效,但python请求不起作用 [英] Curl works but python requests doesn't

查看:78
本文介绍了卷毛有效,但python请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我卷曲时,我会得到回应:

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

但是,当我向相同的网址发出python请求时,状态为410.

However, when I do python requests to the same URL I get a status 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

The host is a standard Ubuntu docker image and only installed Curl and some python modules. Python -V is 2.7

注意:我看着这个问题,但是我无法远程登录到上述服务器,因此该解决方案不适用于我:卷曲有效但Python请求无效

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

推荐答案

您在程序中至少犯了两个错误.

You've made at least two errors in your program.

1)您尚未在 requests.get()调用中指定 data = headers 参数.试试这个:

1) You haven't specified the data= or headers parameters to the requests.get() call. Try this:

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

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

2) .json is a method, not a data attribute of the response object. As a method, it must be called in order to be effective. Try this:

print r.json()

这篇关于卷毛有效,但python请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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