Sign in to follow this  
sunjester

[sunjester tuts] Proxy Rotation with Python

Recommended Posts

Introduction

All of my tutorials are written in a unix environment. If you are using Kali, perfect. I am using Debian (xenial). If you have any errors, post them here and I'll see what I can do. You will need Python and probably will need the requests library installed.

 

Requests

You can install the requests library with pip

pip install requests

and you can import the library using the import keyword.

#!/usr/bin/env python
import requests

Proxies

Using a proxy with the requests library is easy.

#!/usr/bin/env python
import requests

proxies = {
        "socks5": "67.205.174.209:1080"
}

s = requests.get("https://api.ipify.org", proxies=proxies)
print s.content

Rotation

The theory here is that you "rotate" through proxies as you make requests. You could randomly select a proxy from the array or even do it by timeout. The most simple way is to just iterate through the array as you make the requests, by default, I believe that is what the the requests lib does already. So let's pick a random one.

#!/usr/bin/env python
import requests, random, os

proxies = [
        "163.172.220.221:8888",
        "138.68.41.90:3128"
]

proxy = random.choice(proxies)
os.environ['HTTP_PROXY'] = proxy

s = requests.get("https://api.ipify.org")
print s.content

The script above uses the random library to pick a random one from the array and then uses the os library to set an environment variable to the proxy we randomly selected.

Share this post


Link to post
Share on other sites

this is perfect for what i need

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this