Skip to main content

Rotate through IPs in Python using AWS API Gateway.

Project description

requests-ip-rotator

A Python library to utilize AWS API Gateway's large IP pool as a proxy to generate pseudo-infinite IPs for web scraping and brute forcing.

This library will allow the user to bypass IP-based rate-limits for sites and services.


Installation

This package is on pypi so you can install via any of the following:

  • pip3 install requests-ip-rotator
  • python3 -m pip install requests-ip-rotator

 

Simple Usage

import requests
from requests_ip_rotator import ApiGateway

# Create gateway object and initialise in AWS
gateway = ApiGateway("https://site.com")
gateway.start()

# Assign gateway to session
session = requests.Session()
session.mount("https://site.com", gateway)

# Send request (IP will be randomised)
response = session.get("https://site.com/index.html")
print(response.status_code)

# Delete gateways
gateway.shutdown()

Please remember that if gateways are not shutdown via the shutdown() method, you may be charged in future.

 

Costs

API Gateway is free for the first million requests per region, which means that for most use cases this should be completely free.
At the time of writing, AWS charges ~$3 per million requests after the free tier has been exceeded.
 

Documentation

AWS Authentication

It is recommended to setup authentication via environment variables. With awscli, you can run aws configure to do this, or alternatively, you can simply set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY variables yourself.

You can find your access key ID and secret by following the official AWS tutorial.

 

Creating ApiGateway object

The ApiGateway class can be created with the following optional parameters:

Name Description Required Default
site The site (without path) requests will be sent to. True
regions An array of AWS regions to setup gateways in. False ip_rotator.DEFAULT_REGIONS
access_key_id AWS Access Key ID (will override env variables). False Relies on env variables.
access_key_secret AWS Access Key Secret (will override env variables). False Relies on env variables.
from ip_rotator import ApiGateway, EXTRA_REGIONS, ALL_REGIONS

# Gateway to outbound HTTP IP and port for only two regions
gateway_1 = ApiGateway("http://1.1.1.1:8080", regions=["eu-west-1", "eu-west-2"])

# Gateway to HTTPS google for the extra regions pack, with specified access key pair
gateway_2 = ApiGateway("https://www.google.com", regions=EXTRA_REGIONS, access_key_id="ID", access_key_secret="SECRET")

 

Starting API gateway

An ApiGateway object must then be started using the start method.
By default, if an ApiGateway already exists for the site, it will use the existing endpoint instead of creating a new one.
This does not require any parameters, but accepts the following:

Name Description Required
force Create a new set of endpoints, even if some already exist. False
endpoints Array of pre-existing endpoints (i.e. from previous session). False
# Starts new ApiGateway instances for site, or locates existing endpoints if they already exist.
gateway_1.start()

# Starts new ApiGateway instances even if some already exist.
gateway_2.start(force=True)

 

Sending requests

Requests are sent by attaching the ApiGateway object to a requests Session object.
The site given in mount must match the site passed in the ApiGateway constructor.

If you pass in a X-My-X-Forwarded-For header, this will get sent as X-Forwarded-For in the outbound request.

import requests

# Posts a request to the site created in gateway_1. Will be sent from a random IP.
session_1 = requests.Session()
session_1.mount("http://1.1.1.1:8080", gateway_1)
session_1.post("http://1.1.1.1:8080/update.php", headers={"Hello": "World"})

# Send 127.0.0.1 as X-Forwarded-For header in outbound request.
session_1.post("http://1.1.1.1:8080/update.php", headers={"X-My-X-Forwarded-For", "127.0.0.1"})

# Execute Google search query from random IP
session_2 = requests.Session()
session_2.mount("https://www.google.com", gateway_2)
session_2.get("https://www.google.com/search?q=test")

 

Closing ApiGateway Resources

It's important to shutdown the ApiGateway resources once you have finished with them, to prevent dangling public endpoints that can cause excess charges to your account.
This is done through the shutdown method of the ApiGateway object. It will close all resources for the regions specified in the ApiGateway object constructor.

# This will shutdown all gateway proxies for "http://1.1.1.1:8080" in "eu-west-1" & "eu-west-2"
gateway_1.shutdown()

# This will shutdown all gatewy proxies for "https://www.google.com" for all regions in ip_rotator.EXTRA_REGIONS
gateway_2.shutdown()

Credit

The core gateway creation and organisation code was adapter from RhinoSecurityLabs' IPRotate Burp Extension.
The X-My-X-Forwarded-For header forwarding concept was originally conceptualised by ustayready in his fireprox proxy.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requests-ip-rotator-1.0.8.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

requests_ip_rotator-1.0.8-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file requests-ip-rotator-1.0.8.tar.gz.

File metadata

  • Download URL: requests-ip-rotator-1.0.8.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for requests-ip-rotator-1.0.8.tar.gz
Algorithm Hash digest
SHA256 74c26a9190461c43b7ee96853452d7a8da32f68d5b522c18810caf5a8b0f3526
MD5 130ea32009a79f8e74516433d9a7bfaa
BLAKE2b-256 8ffc73507a917d84b46b866aedbc21331a09a59fc8832e9f6c816833e467536f

See more details on using hashes here.

File details

Details for the file requests_ip_rotator-1.0.8-py3-none-any.whl.

File metadata

  • Download URL: requests_ip_rotator-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for requests_ip_rotator-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 20b9ebaea9b7a4cb28d9bb2377a09765b6618aea78c512e227e60e02db173640
MD5 f29009de3cf0a0a16acf0a0da9da97ce
BLAKE2b-256 82987a2b9fc9da31fcef7416a933ad7faca808bccab7a923d105676cf4b57e72

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page