Back to all posts
Tunneling HTTPS Through an HTTP Proxy in Python

Tunneling HTTPS Through an HTTP Proxy in Python

November 22, 2026

Many Python scripts reach for the requests library as soon as they need a proxy. That works well, but it is not always an option. Minimal container images, locked-down build environments, or security policies may block third-party packages. Python's standard library includes http.client, which can tunnel HTTPS through an HTTP proxy without installing anything.

This guide covers one pattern: the HTTP CONNECT tunnel. It shows how to route HTTPS traffic through an HTTP proxy using only http.client, how to add proxy authentication, and how to verify that the exit IP changed. Because the proxy exposes both HTTP and HTTPS through the same HTTP gateway port, the client asks the proxy to open a TCP tunnel to the destination, then performs TLS inside that tunnel.

How the HTTP CONNECT Tunnel Works

An HTTP proxy normally forwards plain HTTP requests. For HTTPS, the client cannot send the final request until the TLS tunnel exists. The CONNECT method tells the proxy to open a raw TCP connection to the target host and port. Once the proxy replies with a 200 status, the client starts a TLS handshake over that socket. Afterward, the proxy forwards encrypted bytes and cannot read the request path, headers, or response body.

The proxy does see the destination host and port in the CONNECT request. It also sees the timing and volume of traffic. For this reason, treat the proxy as a trusted network hop and never send proxy credentials in URLs or log files.

The Standard Library Implementation

The http.client.HTTPSConnection class provides set_tunnel to configure the proxy. The credentials are sent with the Proxy-Authorization header on the CONNECT request.

import base64
import http.client
import json

def fetch_ip_through_proxy(gateway_host, http_port, username, password):
    credentials = base64.b64encode(
        f'{username}:{password}'.encode('utf-8')
    ).decode('ascii')

    conn = http.client.HTTPSConnection('api.ipify.org', timeout=30)
    conn.set_tunnel(
        gateway_host,
        http_port,
        headers={
            'Proxy-Authorization': f'Basic {credentials}'
        }
    )

    conn.request('GET', '/?format=json')
    response = conn.getresponse()
    body = response.read().decode('utf-8')
    conn.close()

    if response.status != 200:
        raise RuntimeError(f'Proxy returned {response.status}: {body}')

    return json.loads(body)['ip']

The target host passed to HTTPSConnection is api.ipify.org, not the proxy. set_tunnel tells http.client to connect to GATEWAY_HOST on HTTP_PORT and send CONNECT api.ipify.org:443. The headers dictionary applies only to that CONNECT request. After the tunnel is open, http.client performs the TLS handshake with api.ipify.org through the proxy socket.

Verifying the Exit IP

Run the function once with the proxy and once without it to confirm that traffic actually leaves through the proxy.

import http.client
import json

direct = http.client.HTTPSConnection('api.ipify.org', timeout=30)
direct.request('GET', '/?format=json')
print('Direct IP:', json.loads(direct.getresponse().read())['ip'])
direct.close()

proxy_ip = fetch_ip_through_proxy(
    'GATEWAY_HOST',
    8080,
    'USERNAME',
    'PASSWORD'
)
print('Proxy IP:', proxy_ip)

If both IPs match, the proxy was not used or the request bypassed it. If they differ, the proxy is handling the request and the destination sees the proxy IP.

Common Errors and What to Check

  • 407 Proxy Authentication Required: the username or password is wrong, or the header is missing. Check USERNAME and PASSWORD.
  • 403 Forbidden: the proxy refuses the target host or the CONNECT method. Confirm that the destination is allowed.
  • 502 Bad Gateway: the proxy could not reach the destination. Check the target address and your network.
  • Connection refused: the wrong GATEWAY_HOST or HTTP_PORT, or the proxy is not accepting connections.
  • Timeout: the proxy or destination is not responding. Increase the timeout value.

Quick cURL Test

Before writing Python, validate the proxy with cURL:

curl -x http://USERNAME:PASSWORD@GATEWAY_HOST:HTTP_PORT https://api.ipify.org?format=json

The -x flag sets the proxy and embeds credentials. You can also split them:

curl -U USERNAME:PASSWORD -x GATEWAY_HOST:HTTP_PORT https://api.ipify.org?format=json

A successful response returns JSON with your exit IP. Use this to confirm the gateway address and port before debugging code.

Configuring a RoProxy Proxy

When you configure a RoProxy proxy, point your client at the gateway host on the HTTP port. The same HTTP port handles both HTTP and HTTPS traffic, so you do not need a separate port for HTTPS. Plans that include SOCKS5 expose SOCKS5_PORT separately, but this guide uses the HTTP gateway port for the CONNECT tunnel.

Conclusion

Using http.client removes the need for an external HTTP library when you must route traffic through a proxy. The CONNECT tunnel plus Proxy-Authorization is enough to reach HTTPS targets and verify the exit IP. Test with cURL first, then move the same host, port, and credentials into your Python code. If you see 407 or 502 responses, check the proxy credentials and gateway address before changing the scraper logic.

RoProxy gives you a username and password per proxy and a gateway host for HTTP and HTTPS traffic. Use those values in place of the placeholders above, and rotate or keep the IP according to your plan's rotation mode.