Yes, you can use proxies with urllib3
. The library provides support for HTTP and SOCKS proxies, which can be configured when creating a PoolManager
or a ProxyManager
instance.
Here's how you can configure urllib3
to use an HTTP proxy:
import urllib3
# Replace 'http://proxyserver:port' with the actual proxy server's URL and port
proxy_url = 'http://proxyserver:port'
# Create a ProxyManager instance with the proxy URL
http = urllib3.ProxyManager(proxy_url)
# Now you can make requests through the proxy
response = http.request('GET', 'http://example.com/')
print(response.data.decode('utf-8'))
For an HTTPS proxy, you need to specify the scheme in the ProxyManager
:
import urllib3
# Replace 'https://proxyserver:port' with the actual HTTPS proxy server's URL and port
proxy_url = 'https://proxyserver:port'
http = urllib3.ProxyManager(proxy_url)
response = http.request('GET', 'https://example.com/')
print(response.data.decode('utf-8'))
If you require a SOCKS proxy, you will need to install PySocks
(or socksipy-branch
) and then use urllib3.contrib.socks
to create a SOCKSProxyManager
:
import urllib3
from urllib3.contrib.socks import SOCKSProxyManager
# Install PySocks if you haven't already:
# pip install pysocks
# Replace 'socks5://proxyserver:port' with the actual SOCKS proxy server's URL and port
proxy_url = 'socks5://proxyserver:port'
# Create a SOCKSProxyManager instance with the proxy URL
http = SOCKSProxyManager(proxy_url)
# Now you can make requests through the SOCKS proxy
response = http.request('GET', 'http://example.com/')
print(response.data.decode('utf-8'))
In all these examples, make sure to replace 'proxyserver:port'
with your proxy server's address and port.
When using proxies, it's also common to encounter SSL verification issues, especially with self-signed certificates. If you trust the proxy server's SSL certificate, you can disable SSL certificate verification by setting the cert_reqs
parameter to 'CERT_NONE'
:
http = urllib3.ProxyManager(proxy_url, cert_reqs='CERT_NONE')
However, disabling SSL verification is not recommended for production code, as it makes your HTTPS requests vulnerable to man-in-the-middle attacks.
Remember that when using proxies, your traffic may be logged or intercepted by the proxy server, so it's important to ensure that the proxy is trustworthy, especially when transmitting sensitive information.