Integrate Urllib3 with ProxyPanel

2023-05-11 14:26

CTRL+K
Thumbnail

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

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 = 'http://httpbin.io/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 http://httpbin.io/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 your Dashboard panel and navigate to the "My Proxy" section to view your IP information.

Click on the "Show Password" button and enter your account password to display your proxy password.

Dashboard-Credentials

By adding urllib3.ProxyManager, you can configure your setup to use a proxy for web requests, enabling secure and anonymous browsing. Below is an example:

import urllib3
 
# Build headers for the basic_auth component
auth_creds = urllib3.util.make_headers(proxy_basic_auth="john.MyProxy-1:Proxypanel!123")
 
# Create a Proxy Manager for managing proxy servers
proxy = urllib3.ProxyManager("http://154.198.34.247:8083", proxy_headers=auth_creds)
 
# Make GET request through the proxy
response = proxy.request("GET", "http://httpbin.io/ip")
 
#Print the returned data
print(response.data.decode('utf-8'))

Replace username, password, proxy_address, and port with your ProxyPanel credentials and proxy details.

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