gpt4 book ai didi

python - 我试图向代理发送 python 请求,但失败了

转载 作者:行者123 更新时间:2023-12-04 04:08:49 26 4
gpt4 key购买 nike

我试图向代理发送 python 请求,但失败了。我做错了什么?

代码如下:

import requests
url = 'https://httpbin.org/ip'

proxies = {
"http": 'http://103.250.153.242',
"https": 'http://103.250.153.242'
}

response = requests.get(url,proxies=proxies)
print(response.json())

结果应该返回 {'origin': '103.250.153.242'} 但我却得到了这个:

Traceback (most recent call last):
File "C:\Python37\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "C:\Python37\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection
raise err
File "C:\Python37\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

我该如何解决这个问题?

最佳答案

import urllib
import requests

r = requests.get('http://google.com', proxies=urllib.request.getproxies())

但是如果你有代理列表:

from urllib3 import ProxyManager, make_headers

default_headers = make_headers(proxy_basic_auth='myusername:mypassword')
http = ProxyManager("https://myproxy.com:8080/", headers=default_headers)

# Now you can use `http` as you would a normal PoolManager
r = http.request('GET', 'https://stackoverflow.com/')

或者:

import requests
proxies = {
'http': 'http://proxy.server:port',
'https': 'http://proxyserver:port',
}
s = requests.Session()
s.proxies = proxies
r = s.get('https://api.ipify.org?format=json').json()

关于python - 我试图向代理发送 python 请求,但失败了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62095446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com