How to use proxy in the program?

How can I use a proxy when visiting websites?

Current behavior:
I have tried both http and https proxies, neither of them can work porperly.Is this because that replit dont allow this kind of connection?

Desired behavior

Repl link:

if __name__ == '__main__':
  # main()
  url = "http://httpbin.org/"
  proxy = "127.0.0.1:8888"
  proxies = {"http": "http://{proxy}".format(proxy=proxy)}
  HEADER = {
      'User-Agent':
      'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
      'Accept': '*/*',
      'Connection': 'keep-alive',
      'Accept-Language': 'zh-CN,zh;q=0.8'
  }
  r = head(url, headers=HEADER, proxies=proxies)
  print(r.status_code)

And below is the wrong code:

Traceback (most recent call last):
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/connection.py", line 203, in _new_conn
    sock = connection.create_connection(
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/util/connection.py", line 85, in create_connection
    raise err
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/util/connection.py", line 73, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/connectionpool.py", line 790, in urlopen
    response = self._make_request(
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/connectionpool.py", line 496, in _make_request
    conn.request(
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/connection.py", line 395, in request
    self.endheaders()
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/http/client.py", line 1278, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/http/client.py", line 976, in send
    self.connect()
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/connection.py", line 243, in connect
    self.sock = self._new_conn()
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/connection.py", line 218, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f1e1a543250>: Failed to establish a new connection: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

urllib3.exceptions.ProxyError: ('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1e1a543250>: Failed to establish a new connection: [Errno 111] Connection refused'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/requests/adapters.py", line 486, in send
    resp = conn.urlopen(
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/connectionpool.py", line 844, in urlopen
    retries = retries.increment(
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/urllib3/util/retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=8888): Max retries exceeded with url: http://httpbin.org/ (Caused by ProxyError('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1e1a543250>: Failed to establish a new connection: [Errno 111] Connection refused')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/Proxy-Pool/main.py", line 40, in <module>
    r = head(url, headers=HEADER, proxies=proxies)
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/requests/api.py", line 100, in head
    return request("head", url, **kwargs)
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "/home/runner/Proxy-Pool/.pythonlibs/lib/python3.10/site-packages/requests/adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='127.0.0.1', port=8888): Max retries exceeded with url: http://httpbin.org/ (Caused by ProxyError('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1e1a543250>: Failed to establish a new connection: [Errno 111] Connection refused')))

Welcome to the forums, @LeonardoZhou!
Replit does not allow for proxies.

1 Like

*For the intent of accessing unauthorized resources at school.

Idk if this is one of those, but it didn’t give off that energy. “Proxy pool” is quite a general term in proxies so it isn’t the name of a specific project.

1 Like

To be clear, @LeonardoZhou , proxies are not allowed if tey are used to bypass school restrictions or for malicious reasons. I don’t think this looks very malicious.

1 Like

Thanks for answering my question.

Thanks for answering. Is there any interface or switches that can enable proxies? I just want to test if the proxy is avaliable.

You can run the repl to test the program. There isn’t a switch, but you can run your repl.