简体   繁体   English

Curl 有效,但 Python 请求无效

[英]Curl works but not Python requests

I am trying to fetch a JSON response from http://erdos.sdslabs.co/users/shagun.json .我正在尝试从http://erdos.sdslabs.co/users/shagun.json获取 JSON 响应。 Using browser/Python's Requests library leads to an authentication error, but curl seems to work fine.使用浏览器/Python 的请求库会导致身份验证错误,但 curl 似乎工作正常。

curl http://erdos.sdslabs.co/users/shagun.json 

returns the JSON response.返回 JSON 响应。

Why would the curl request work while a normal browser or Requests-based request fail?为什么 curl 请求会在普通浏览器或基于请求的请求失败时工作?

Using telnet to check:使用telnet检查:

$ telnet erdos.sdslabs.co 80
Trying 62.141.37.215...
Connected to erdos.sdslabs.co.
Escape character is '^]'.
GET http://erdos.sdslabs.co/users/shagun.json HTTP/1.0

HTTP/1.1 302 Found
Date: Sat, 26 Jul 2014 11:18:58 GMT
Server: Apache
Set-Cookie: PHPSESSID=juvg7vrg3vs4t00om3a95m4sc7; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: /login
Access-Control-Allow-Origin: http://erdos.sdslabs.co
X-Powered-By: PleskLin
Content-Length: 1449
Connection: close
Content-Type: application/json

{"email":"sshagun.sodhani@gmail.com","username":"shagun","name":"Shagun      
[...]

We see that the web server is responding with a 302 - a redirection to Location /login.我们看到 Web 服务器正在响应 302 - 重定向到 Location /login。 Requests and web browsers are obeying that, and reaching the login prompt.请求和 Web 浏览器都遵守这一点,并到达登录提示。 However, we see that the web server is also responding with the json you're after, and curl (and telnet) are simple enough to just accept that data.但是,我们看到 Web 服务器也在使用您想要的 json 进行响应,并且 curl(和 telnet)非常简单,可以只接受该数据。

Best practice would be to fix the web server so that it either doesn't require you to log in, or doesn't give out password-protected data at the same time as asking users to log in.最佳做法是修复 Web 服务器,使其不需要您登录,或者在要求用户登录的同时不提供受密码保护的数据。

If you can't change the web server, you could tell the requests module to ignore redirects:如果您无法更改 Web 服务器,您可以告诉 requests 模块忽略重定向:

import requests
result = requests.get('http://erdos.sdslabs.co/users/shagun.json', allow_redirects=False)
print result.content

In case you have a proxy configured at your environment, define it at your session/request as well.如果您在您的环境中配置了代理,请在您的会话/请求中也定义它。

For example with session:例如会话:

    my_proxies = {  
        'http': 'http://myproxy:8080',  
        'https': 'https://myproxy:8080'  
    }

    session = requests.Session()  
    request = requests.Request('POST', 'http://my.domain.com', data=params_template, headers=req_headers, proxies=my_proxies)  
    prepped = session.prepare_request(request)  
    response = session.send(prepped)  

see documentation:见文档:
request http://docs.python-requests.org/en/master/user/quickstart/请求http://docs.python-requests.org/en/master/user/quickstart/
session http://docs.python-requests.org/en/master/user/advanced/会话http://docs.python-requests.org/en/master/user/advanced/

For late googlers like myself:对于像我这样的已故谷歌员工:

In my case, the problem was that I provided url params using requests.get(url, data={...}) .就我而言,问题是我使用requests.get(url, data={...})提供了 url 参数。 After changing it to requests.get(url, params={...}) , the problem was solved. requests.get(url, params={...}) ,问题解决。

I had the experience that some python requests code that had worked previously one day didn't come back the next, while curl was still working.我的经验是,一些 Python 请求前一天工作的代码在第二天没有回来,而 curl 仍在工作。 It wasn't the code, and it wasn't the server, and reading this discussion it dawned on me that something in the connection may have changed.这不是代码,也不是服务器,阅读此讨论后,我突然意识到连接中的某些内容可能已更改。 I disabled and re-enabled my Wifi, and lo and behold, it worked again.我禁用并重新启用了我的 Wifi,瞧,它又工作了。

I didn't investigate further, requests may have cached something that wasn't valid any more.我没有进一步调查,请求可能缓存了一些不再有效的东西。 Sorry about this unqualified input, but maybe it will help someone out there.很抱歉这个不合格的输入,但也许它会帮助那里的人。

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

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