Sous proxy / Integrations / Python urllib

Proxy in urllib from the standard library

When you can't install packages, urllib.request is enough. Here is how to send a request through an HTTP proxy with a password.

HTTPHTTPSLogin and passwordRotation and sticky
Python urllib · proxy
http://LOGIN:PASSWORD@HOST:PORT
http://LOGIN:PASSWORD@HOST:PORT
✓ IP: Germany, residentialRun

Why use a proxy in Python urllib

urllib.request is the Python standard library module for HTTP requests. A proxy is attached via ProxyHandler; login and password can go in the proxy URL. The module doesn't support SOCKS.

What to pick

Take an HTTP proxy: urllib has no SOCKS. For one-off scripts and checks rotation is enough. If several requests must come from one address, enable a sticky session.

Residential

from $0.6 / GB

Home ISP addresses. The main choice for accounts and protected sites.

VIP

$2 / GB

Hand-picked clean IPs, faster and more stable than regular residential.

Mobile

$4 / GB

Carrier IPs, for platforms that check addresses strictly.

Step-by-step setup

Button names are given exactly as they appear in Python urllib.

  1. Install nothing

    urllib.request ships with every Python 3.

  2. Export the proxy

    Take the login:pass@ip:port format in @SousProxyBot and prepend http://. Encode special characters in the password with urllib.parse.quote.

  3. Create a ProxyHandler

    Pass one string for the "http" and "https" keys. The key is the site's scheme, not the proxy protocol.

  4. Build an opener

    urllib.request.build_opener(handler) returns an object that sends requests. For a proxy across the whole script, call install_opener(opener).

  5. Check the IP

    Run python ip.py; the response contains the proxy IP.

Code example

Put in your own HOST, PORT, LOGIN and PASSWORD from the bot or the dashboard.

python
import json
import urllib.request

proxy = "http://LOGIN:PASSWORD@HOST:PORT"
handler = urllib.request.ProxyHandler({"http": proxy, "https": proxy})
opener = urllib.request.build_opener(handler)

with opener.open("https://api.ipify.org?format=json", timeout=15) as resp:
    print(json.load(resp))

Tested by running it

We ran this example ourselves: Python 3.13.18 · urllib.request (stdlib). Real output below, IP hidden.

Terminal · ip.py
$ python ip.py
{'ip': 'proxy IP'}

And this is the response when the password is wrong:

$ python ip.py
…
                        ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/urllib/request.py", line 1334, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error Tunnel connection failed: 407 Proxy Authentication Required>

Common errors

The issues people most often bring to support.

!

HTTP Error 407: Proxy Authentication RequiredCheck the login and password in the URL. If the password contains @, : or /, encode them.

!

HTTPS requests bypass the proxyThe ProxyHandler dict has no "https" key. Add it with the same string.

!

Requests to some addresses go directurllib honours the no_proxy variable. Check whether the domain ended up there.

!

You need SOCKS5urllib can't do it. Take an HTTP proxy or switch to requests or httpx, which have SOCKS.

Need a proxy for Python urllib?

Residential, VIP and mobile IPs. HTTP and SOCKS5, pay per GB, 1 GB free to try.

FAQ

Can I skip creating an opener?

Yes, urllib.request.urlopen reads the http_proxy and https_proxy variables. An explicit ProxyHandler is clearer and doesn't depend on the environment.

How do I set the proxy for the whole script?

Call urllib.request.install_opener(opener). After that a plain urlopen goes through the proxy.

How do I test the proxy separately from the code?

Run the line through /en/tools/proxy-checker/.