ProxyForge Download

httpcore and Proxies: Low-Level HTTP in Python for Scraping

Developer Guide: httpcore and Proxies for Low-Level HTTP in Python

HTTP requests are a fundamental part of web scraping, automation, and API communication. For developers who need precise control over their network interactions, httpcore is an essential tool. This guide will explore how to use httpcore with proxies, covering both synchronous and asynchronous operations, as well as HTTP/1.1 and HTTP/2 support.

Introduction

When you're working on large-scale projects like web scraping or automation, it's crucial to have full control over your network connections. httpcore is a low-level HTTP transport library that forms the foundation of httpx, a popular high-level asynchronous HTTP client. By using httpcore directly, you gain access to advanced features such as fine-grained connection pooling and precise timeout control.

This guide will walk you through setting up proxies with httpcore, configuring both synchronous and asynchronous pools, handling HTTPS connections through HTTP proxies, and implementing custom transports for scenarios where httpx's API falls short.

Setting Up Proxies with httpcore

Synchronous Connection Pool

To use a proxy with httpcore, you first need to set up a SyncConnectionPool. Here's how you can do it:

import httpcore

# Define your proxy and target URL
proxy = httpcore.Proxy('http://your-proxy-host:port')
url = 'http://example.com'

# Create a synchronous connection pool with the proxy
pool = httpcore.SyncConnectionPool(proxy=proxy)

# Make a request
with pool.stream("GET", url) as response:
    status_code = response.status_code
    headers = response.headers
    content = response.read()

print(f'Status Code: {status_code}')
print('Headers:', dict(headers))
print('Content:', content)

Asynchronous Connection Pool

For asynchronous operations, use AsyncConnectionPool:

import httpcore
import asyncio

async def fetch(url):
    proxy = httpcore.Proxy('http://your-proxy-host:port')
    pool = httpcore.AsyncConnectionPool(proxy=proxy)

    async with pool.stream("GET", url) as response:
        status_code = response.status_code
        headers = response.headers
        content = await response.aread()

    print(f'Status Code: {status_code}')
    print('Headers:', dict(headers))
    print('Content:', content)

# Run the asynchronous fetch function
asyncio.run(fetch('http://example.com'))

HTTP/2 Support

httpcore also supports HTTP/2, which can be beneficial for performance. Here's how to set up an HTTP/2 connection pool:

import httpcore

# Define your proxy and target URL
proxy = httpcore.Proxy('http://your-proxy-host:port')
url = 'https://example.com'

# Create a synchronous HTTP/2 connection pool with the proxy
pool = httpcore.HTTP2ConnectionPool(proxy=proxy)

# Make a request
with pool.stream("GET", url) as response:
    status_code = response.status_code
    headers = response.headers
    content = response.read()

print(f'Status Code: {status_code}')
print('Headers:', dict(headers))
print('Content:', content)

Tunnel Connect for HTTPS Through HTTP Proxy

httpcore automatically handles the CONNECT handshake required for HTTPS requests through an HTTP proxy. This means you can seamlessly switch between HTTP and HTTPS without additional configuration.

Custom Transport for httpx

If httpx's API doesn't provide the level of control you need, you can create a custom transport using httpcore:

import httpcore
import httpx

# Define your proxy
proxy = httpcore.Proxy('http://your-proxy-host:port')

# Create a synchronous connection pool with the proxy
pool = httpcore.SyncConnectionPool(proxy=proxy)

# Create a custom transport
class CustomTransport(httpx.BaseTransport):
    def handle_request(self, request):
        with pool.stream(request.method, request.url, headers=request.headers) as response:
            return httpx.Response(
                status_code=response.status_code,
                headers=dict(response.headers),
                content=response.read(),
            )

# Use the custom transport in httpx
transport = CustomTransport()
client = httpx.Client(transport=transport)

response = client.get('http://example.com')
print(response.text)

Conclusion

While httpx provides a high-level and user-friendly API for making HTTP requests, there are scenarios where you need more control. Using httpcore directly gives you access to advanced features such as fine-grained connection pooling, precise timeout control, and the ability to handle HTTP/2 connections.

When deciding whether to use httpx or httpcore, consider your specific needs:

Remember that managing proxy lists manually can be cumbersome. If you're looking for a solution that auto-tests and rotates proxies locally on your machine, check out ProxyForge. It's a one-off $29 purchase with no subscription required: https://proxyforge.dev/#download.

For professional or managed proxy services, established providers like Bright Data and SmartProxy offer reliable rotating proxies suitable for production use.

By leveraging httpcore, you can build robust and efficient HTTP clients tailored to your specific requirements.

Free proxies break. ProxyForge doesn't.

The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.

Download ProxyForge free →