Introduction

Urllib3 — An Overview

urllib3 is a powerful, user-friendly HTTP library for Python. It supports features such as connection pooling, client-side SSL/TLS verification, and proxy support. Its robust error-handling capabilities and compatibility with other Python libraries make it an excellent choice for web scraping and interacting with web APIs. For more information, visit Urllib3 documentation.

Prerequisites

Install Python from the official website.

Check if Python is installed correctly, you can use the following command in your terminal or command prompt

python --version

The output should be in the following format:

Python 3.11.2

If you receive an error or see a version number starting with 2.x, you need to download Python 3.x and follow the installation instructions to set it up

Next, create a new Python project and install Urllib3 package

pip install urllib3

urllib3 only talks to SOCKS proxies with the help of PySocks. If you want to use the SOCKS5 port, install the package with its SOCKS extra instead:

pip install "urllib3[socks]"

After installing Urllib3 library, let's make a request to get our IP address. This can be done by sending a GET request to a service that returns your public IP. Here’s how you can do it:

import urllib3

# Initialize a PoolManager instance for sending requests
http = urllib3.PoolManager()

# URL to make the request to
url = 'https://httpbin.org/ip'

# Send a GET request to the URL
response = http.request('GET', url)

# Check if the request was successful
if response.status == 200:
    # Print the response data
    print(response.data.decode('utf-8'))
else:
    # Print the status code if the request failed
    print(f"Request failed with status code: {response.status}")

After running the script, it sends a GET request to https://httpbin.org/ip and retrieves your public IP address from the response. The IP address is then printed out in JSON format.

Now, we've used urllib3 without integrating ProxyPanel proxies. Follow the next steps to configure your proxy with urllib3.

Configuring the Urllib3 Package with ProxyPanel

Go to My Proxies in your dashboard. Make sure the proxy has been started: one that still reads Pending needs a press of Start proxy.

The Connection column lists two addresses for the proxy. "HTTP proxy" is the host with the HTTP port; "SOCKS5 proxy" is the same host with the SOCKS5 port. In the Credentials column, Copy username copies the username, and the password is revealed with the eye button (Show / hide) or copied with Copy password. No account password is required.

Store the details as environment variables before running the examples:

# macOS / Linux
export PROXY_HOST="HOST"
export PROXY_HTTP_PORT="HTTP_PORT"
export PROXY_SOCKS5_PORT="SOCKS5_PORT"   # only needed for SOCKS5
export PROXY_USERNAME="USERNAME"
export PROXY_PASSWORD="PASSWORD"

# Windows (PowerShell)
$env:PROXY_HOST = "HOST"
$env:PROXY_HTTP_PORT = "HTTP_PORT"
$env:PROXY_SOCKS5_PORT = "SOCKS5_PORT"
$env:PROXY_USERNAME = "USERNAME"
$env:PROXY_PASSWORD = "PASSWORD"

By adding urllib3.ProxyManager, you can configure your setup to use a proxy for web requests, enabling secure and anonymous browsing. ProxyManager works with the HTTP port, and the login goes in a Proxy-Authorization header built by make_headers. Below is an example:

import os

import urllib3

# Build headers for the basic_auth component (plain username:password, no encoding)
auth_creds = urllib3.util.make_headers(
    proxy_basic_auth=f"{os.environ['PROXY_USERNAME']}:{os.environ['PROXY_PASSWORD']}"
)

# Create a Proxy Manager for the HTTP port
proxy = urllib3.ProxyManager(
    f"http://{os.environ['PROXY_HOST']}:{os.environ['PROXY_HTTP_PORT']}",
    proxy_headers=auth_creds,
)

# Make GET request through the proxy
response = proxy.request("GET", "https://httpbin.org/ip")

#Print the returned data
print(response.data.decode('utf-8'))

The script reads every proxy detail from the environment variables, so there's nothing to edit in it.

You will notice that the IP address with proxy configuration differs from the one without a proxy. Now you have successfully integrated the urllib3 package with ProxyPanel.

For the SOCKS5 port, use SOCKSProxyManager (ProxyManager doesn't accept SOCKS URLs). It needs the SOCKS extra from the prerequisites. The socks5h scheme has the proxy resolve hostnames, and the username and password go in as separate arguments:

import os

from urllib3.contrib.socks import SOCKSProxyManager

socks_proxy = SOCKSProxyManager(
    f"socks5h://{os.environ['PROXY_HOST']}:{os.environ['PROXY_SOCKS5_PORT']}",
    username=os.environ['PROXY_USERNAME'],
    password=os.environ['PROXY_PASSWORD'],
)

response = socks_proxy.request("GET", "https://httpbin.org/ip")
print(response.data.decode('utf-8'))