'Python Proxies Returning Connection Error

I've seen hundreds of posts with this same question but none of the answers ever work. I've tried doing everything on the post answers and they all don't work.

import requests
import random

def get_proxies():
  request = requests.get('https://proxylist.geonode.com/api/proxy-list?limit=50&page=1&sort_by=lastChecked&sort_type=desc')
  request = request.json()['data']
  proxies = []
  for proxy in request:
    proxies.append(f'{proxy["ip"]}:{proxy["port"]}')
  return proxies

def format_proxy(proxy):
  return {'http': proxy, 'https': proxy}

if __name__ == '__main__':
  proxies = get_proxies()
  response =requests.get('https://api.ipify.org/', proxies=format_proxy(random.choice(proxies)), headers={"User-Agent": "Opera/9.80 (X11; Linux x86_64; U; de) Presto/2.2.15 Version/10.00"})
  print(response.text)

I'm scraping the proxies from https://geonode.com/free-proxy-list/ and I keep getting the same error. I'm scraping new proxies every time I send a request so they shouldn't be invalid yet it's giving me a "max retry exceeded error"

  File "/home/runner/proxies/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 700, in urlopen
    self._prepare_proxy(conn)
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 994, in _prepare_proxy
    conn.connect()
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/urllib3/connection.py", line 369, in connect
    self._tunnel()
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/http/client.py", line 901, in _tunnel
    (version, code, message) = response._read_status()
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/http/client.py", line 277, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/socket.py", line 669, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.ipify.org', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(104, 'Connection reset by peer')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 19, in <module>
    response =requests.get('https://api.ipify.org/', proxies=proxy, headers={"User-Agent": "Opera/9.80 (X11; Linux x86_64; U; de) Presto/2.2.15 Version/10.00"})
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/home/runner/proxies/venv/lib/python3.8/site-packages/requests/adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.ipify.org', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(104, 'Connection reset by peer')))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source