英文:
Max retries exceeded with url error Python Requests
问题
以下是翻译好的部分:
代码:
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
def web():
rq = requests.get("https://myurl.com/gts?search=word", headers=headers)
print(rq)
DigitalOcean 中的错误:
HTTPSConnectionPool(host='myurl.com', port=443): Max retries exceeded with url: /gts?search=word (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7fa364a89690>, 'Connection to myurl.com timed out. (connect timeout=None)'))
我尝试但失败的操作:
我添加了 verify=False
的代码,从控制台禁用了防火墙。并尝试使用 "curl -I myurl.com" 命令从控制台访问该网站。所有尝试都失败。
感谢您的帮助。
英文:
I'm trying to connect to a website with the code below. There is no problem on Heroku. But I am getting error in DigitalOcean.
Code:
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
def web():
rq = requests.get("https://myurl.com/gts?search=word", headers = headers)
print(rq)
The error I get in DigitalOcean:
HTTPSConnectionPool(host='myurl.com', port=443): Max retries exceeded with url: /gts?search=word (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7fa364a89690>, 'Connection to myurl.com timed out. (connect timeout=None)'))
What I've done and failed:
I added code verify=False, disabled firewall from console. And I tried to access the site from the console with the "curl -I myurl.com" command. All failed.
Thank you for your help.
答案1
得分: 1
似乎问题不是由于你的代码中存在错误,而是由于远程服务器 (sozluk.gov.tr
) 上的防火墙(或其他规则)引起的。
我尝试在本地以及远程(Digital Ocean 托管的)虚拟机上运行了 curl -I https://sozluk.gov.tr
和你提供的代码段 - 两者都正常工作。
你提到从控制台运行的 curl
命令(我假设是远程虚拟机控制台)没有工作。这表明问题在网络层面,而不是代码层面。
我建议在不同的区域(以获得来自不同IP池的IP)中启动一个新的虚拟机,或者使用一种远程服务器没有阻止的代理。你可以在此链接上查看可用的区域(数据中心)。
英文:
It seems like the issue is not caused by a bug in your code, but rather by firewall (or other rules) on the remote server side (sozluk.gov.tr
).
I have tried to run curl -I https://sozluk.gov.tr
and also your snippet on my local and also remote (Digital Ocean hosted) VM - both worked fine.
You mentioned that curl
command from console (I assume remove VM console) did not work. This indicates, that the issue is on the network - rather than code - level.
I recommend to spin new VM in different region (to get IP from different IP pool) or use some kind of a proxy which is not blocked by remote server. You can check available regions (datacenters) on this link.
Responses
user@do-server:~$ curl -I https://sozluk.gov.tr
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 18 Feb 2023 11:18:37 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 108975
Last-Modified: Fri, 13 Jan 2023 12:38:15 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "63c150b7-1a9af"
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Accept-Ranges: bytes
user@do-server:~$ python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
>>> requests.get("https://sozluk.gov.tr/gts?search=word", headers = headers)
<Response [200]>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论