Proxies
The httpcorexyz package provides support for HTTP proxies, using either "HTTP Forwarding" or "HTTP Tunnelling". Forwarding is a proxy mechanism for sending requests to http URLs via an intermediate proxy. Tunnelling is a proxy mechanism for sending requests to https URLs via an intermediate proxy.
Sending requests via a proxy is very similar to sending requests using a standard connection pool:
import httpcorexyz
proxy = httpcorexyz.Proxy("http://127.0.0.1:8080/")
pool = httpcorexyz.ConnectionPool(proxy=proxy)
r = proxy.request("GET", "https://www.example.com/")
print(r)
# <Response [200]>
You can test the httpcorexyz proxy support, using the Python proxy.py tool:
$ pip install proxy.py
$ proxy --hostname 127.0.0.1 --port 8080
Requests will automatically use either forwarding or tunnelling, depending on if the scheme is http or https.
Authentication
Proxy authentication can be included in the initial configuration:
import httpcorexyz
# A `Proxy-Authorization` header will be included on the initial proxy connection.
proxy = httpcorexyz.Proxy(
url="http://127.0.0.1:8080/",
auth=("<username>", "<password>")
)
pool = httpcorexyz.ConnectionPool(proxy=proxy)
Custom headers can also be included:
import httpcorexyz
import base64
# Construct and include a `Proxy-Authorization` header.
auth = base64.b64encode(b"<username>:<password>")
proxy = httpcorexyz.Proxy(
url="http://127.0.0.1:8080/",
headers={"Proxy-Authorization": b"Basic " + auth}
)
pool = httpcorexyz.ConnectionPool(proxy=proxy)
Proxy SSL
The httpcorexyz package also supports HTTPS proxies for http and https destinations.
HTTPS proxies can be used in the same way that HTTP proxies are.
proxy = httpcorexyz.Proxy(url="https://127.0.0.1:8080/")
Also, when using HTTPS proxies, you may need to configure the SSL context, which you can do with the ssl_context argument.
import ssl
import httpcorexyz
proxy_ssl_context = ssl.create_default_context()
proxy_ssl_context.check_hostname = False
proxy = httpcorexyz.Proxy(
url='https://127.0.0.1:8080/',
ssl_context=proxy_ssl_context
)
pool = httpcorexyz.ConnectionPool(proxy=proxy)
HTTP Versions
If you use proxies, keep in mind that the httpcorexyz package only supports proxies to HTTP/1.1 servers.
SOCKS proxy support
The httpcorexyz package also supports proxies using the SOCKS5 protocol.
Make sure to install the optional dependency using pip install 'httpcorexyz[socks]'.
The SOCKSProxy class should be using instead of a standard connection pool:
import httpcorexyz
# Note that the SOCKS port is 1080.
proxy = httpcorexyz.Proxy(url="socks5://127.0.0.1:1080/")
pool = httpcorexyz.ConnectionPool(proxy=proxy)
r = pool.request("GET", "https://www.example.com/")
Authentication via SOCKS is also supported:
import httpcorexyz
proxy = httpcorexyz.Proxy(
url="socks5://127.0.0.1:1080/",
auth=("<username>", "<password>"),
)
pool = httpcorexyz.ConnectionPool(proxy=proxy)
r = pool.request("GET", "https://www.example.com/")