尝试在本地网站上使用 python 请求时出现 503 响应

2024-05-02

我正在尝试从本地服务器上抓取我自己的网站。但是当我对它使用 python requests 时,它给我一个响应 503。网络上的其他普通站点可以工作。有什么原因/解决方案吗?

import requests 
url = 'http://127.0.0.1:8080/full_report/a1uE0000002vu2jIAA/'

r = requests.get(url)

print r

打印出来

<Response [503]>

经过进一步调查,我发现了与我类似的问题。Python 尝试访问 localhost:8000 时请求 503 错误 https://stackoverflow.com/questions/20832298/python-requests-503-erros-when-trying-to-access-localhost8000

不过,我认为他还没有解决这个问题。我可以通过网络浏览器访问本地网站,但无法使用 requests.get 函数访问。我还使用 Django 来托管服务器。

python manage.py runserver 8080 

当我使用时: 卷曲-vvvhttp://127.0.0.1:8080 http://127.0.0.1:8080

* Rebuilt URL to: http://127.0.0.1:8080/
*   Trying 10.37.135.39...
* Connected to proxy.kdc.[company-name].com (10.37.135.39) port 8099 (#0)
* Proxy auth using Basic with user '[company-id]'
> GET http://127.0.0.1:8080/ HTTP/1.1
> Host: 127.0.0.1:8080
> Proxy-Authorization: Basic Y2FhNTc2OnJ2YTkxQ29kZQ==
> User-Agent: curl/7.49.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: BlueCoat-Security-Appliance
< Location:http://10.118.216.201
< Connection: Close
<
<HTML>
<HEAD><TITLE>Redirection</TITLE></HEAD>
<BODY><H1>Redirect</H1></BODY>
* Closing connection 0

我无法使用 python requests 请求本地 url,因为公司的网络软件不允许。这是一条死胡同,必须寻求其他途径。

编辑:工作解决方案

>>> import requests
>>> session = requests.Session()
>>> session.trust_env = False
>>> r = session.get("http://127.0.0.1:8080")
>>> r
<Response [200]>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试在本地网站上使用 python 请求时出现 503 响应 的相关文章

随机推荐