Я попытался отправить запрос python на прокси, но это не удалось - PullRequest
2 голосов
/ 30 мая 2020

Я попытался отправить запрос 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

Как это исправить?

1 Ответ

0 голосов
/ 30 мая 2020
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()
...