Skip to content

IPRoyal/how-to-use-iproyal-proxies-with-python-requests

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

How to Use IPRoyal Proxies With Python Requests

GitHub Banner

requests is a simple Python HTTP client for making web requests. It supports proxy configuration and is widely used for automation, API testing, and scraping tasks.
Though not asynchronous, it’s lightweight and easy to extend.

Setting Up IPRoyal Proxies

  1. Create an account on the IPRoyal Dashboard.
    Register
  2. Purchase Residential Proxies → Click Buy Now.
    Buy
  3. Choose the required traffic amount.
    Traffic
  4. Complete payment and proceed to your proxy dashboard.
    Payment
  5. Select the user:pass@host:port format for Python Requests.
    Format

Alternatively, you may use IP whitelisting to bypass authentication credentials. This option is ideal for fixed IP environments.
Whitelist

Installation

Install the requests library:

pip install requests

Usage Examples

1. Basic Proxy Authentication

Use standard username and password authentication for your proxy connection.

import requests
proxy = "http://USER:PASS@HOST:PORT"

# Proxy dictionary to be passed to requests
proxies = {
    "http": proxy,
    "https": proxy,
}

# Example URL to test the proxy
url = "http://httpbin.org/ip"

# Make a request using the proxy
response = requests.get(url, proxies=proxies)

# Print the response
print(response.json())

2. IP Whitelisting Authentication

If you have whitelisted your IP address on the dashboard, you can connect without credentials.

import requests
proxy = "http://HOST:PORT"

# Proxy dictionary to be passed to requests
proxies = {
    "http": proxy,
    "https": proxy,
}

# Example URL to test the proxy
url = "http://httpbin.org/ip"

# Make a request using the proxy
response = requests.get(url, proxies=proxies)

# Print the response
print(response.json())

3. Proxy Rotation

If your provider doesn’t handle rotation automatically, you can rotate manually across multiple proxies. This example iterates through each proxy and retries on failure.

import requests

# List of proxies provided by IPRoyal
proxies_list = [
    {"host": "geo.iproyal.com", "port": "12321", "username": "user1", "password": "pass1"},
    {"host": "geo.iproyal.com", "port": "12321", "username": "user2", "password": "pass2"},
    {"host": "geo.iproyal.com", "port": "12321", "username": "user3", "password": "pass3"},
    # Add more proxies as needed
]

def get_proxy_url(proxy):
    return f"http://{proxy['username']}:{proxy['password']}@{proxy['host']}:{proxy['port']}"

def make_request(url, proxies_list):
    for proxy in proxies_list:
        proxy_url = get_proxy_url(proxy)
        proxies = {
            "http": proxy_url,
            "https": proxy_url,
        }

        try:
            response = requests.get(url, proxies=proxies, timeout=10)
            response.raise_for_status()  # Raise an HTTPError for bad responses
            print(response.json())
        except requests.exceptions.RequestException as e:
            print(f"An error occurred with proxy {proxy['host']}:{proxy['port']}: {e}")

# Example URL to test the proxies
url = "http://httpbin.org/ip"

# Make requests using different proxies
make_request(url, proxies_list)

About

This repository demonstrates how to integrate IPRoyal Residential Proxies with the Python requests library for secure, authenticated, and rotating web requests.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages