本文介绍了卷曲和Python请求(获取)报告了不同的http状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个python脚本来验证主机的url连接。在linux curl 中报告成功(http 200)的内容在python(3.6) requests 模块中被报告为403 。

I have written a python script to validate url connectivity from a host. What is reporting successful (http 200) in linux curl is reported as a 403 in the python (3.6) requests module.

我希望有人可以帮助我了解所报告的http状态代码中的区别吗?

I'm hoping someone can help me understand the differences here in reported http status codes?

从Linux命令行卷曲....

Curl from the Linux command line....

$ curl -ILs https://www.h2o.ai|egrep ^HTTP
HTTP/1.1 200 OK

Python请求模块.....

Python requests module.....

>>> import requests
>>> url = 'https://www.h2o.ai'
>>> r = requests.get(url, verify=True, timeout=3)
>>> r.status_code
403
>>> requests.packages.urllib3.disable_warnings()
>>> r = requests.get(url, verify=False, timeout=3)
>>> r.status_code
403


推荐答案

python-requests /< version> User-Agent 正在收到该站点的403响应:

It seems the python-requests/<version> User-Agentis being served the 403 response from the site:

In [98]: requests.head('https://www.h2o.ai', headers={'User-Agent': 'Foo bar'})
Out[98]: <Response [200]>

In [99]: requests.head('https://www.h2o.ai')
Out[99]: <Response [403]>

如果需要,您可以联系网站所有者,也可以通过<$使用其他用户代理c $ c> User-Agent 标头(如我上面使用的一样)。

You can contact the site owner if you want or just use a different user-agent via the User-Agent header (like i used above).

我如何调试它:

我已经运行了 curl -v -verbose )选项检查发送的标头,然后使用 requests 使用 response.request (假定响应另存为 response )。

I have run curl with -v (--verbose) option to check the headers being sent, and then checked out the same with requests using response.request (assuming the response is saved as response).

除了 User-Agent 标头外,我没有发现任何明显的区别;因此,更改 User-Agent 标头的工作符合我的预期。

I did not find any significant difference apart from the User-Agent header; hence, changing the User-Agent header worked as i expected.

这篇关于卷曲和Python请求(获取)报告了不同的http状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:40