Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Kashif Aziz
Kashif Aziz

Posted on

     

Proxy Server Rotation Script in Python

Proxy Server Rotation Script in Python

Rotating Proxy Servers in Python

Recently, I have used ProxyMesh proxy server for a project. ProxyMesh offers 15 proxy servers, each denoting a specific location (such as us-dc) with 10 IP addresses rotating twice per day.

However, the free trial allows you to have one proxy server at a time. This means that if you are working with a server or CDN that strictly throttles IPs, you have to change the proxy server manually to keep rotating between the 15 proxy servers.

Fortunately, ProxyMesh provides an API that can be used to add and delete the proxy servers in user dashboard. Once a proxy server is assigned to the user, it can be fetched and used. All we need is a script that rotates between the proxy servers, deleting and adding them as required.

Based on this, I have written a script that would rotate through ProxyMesh proxy servers using their API.

Prerequisites

An account with ProxyMesh, either free trial or paid. Set the user name and password in rotatingproxy.py

self.user = ""self.password = ""
Enter fullscreen modeExit fullscreen mode

Usage

Setting the Proxy Server

from rotatingproxy import RotatingProxyrproxy = RotatingProxy()
Enter fullscreen modeExit fullscreen mode

The proxy server can either be set randomly or selected from an available list of proxy servers.
The active proxy server is saved in a text file which can be accessed as required.

rproxy.set_proxy(israndom="r")  # select a random proxy serverrproxy.set_proxy(proxy_num=1)   # select proxy server with index=1 from the list of proxy servers.
Enter fullscreen modeExit fullscreen mode

Accessing the Proxy Server

def get_proxy_from_file():    # fetches proxy from proxy.txt    with open("proxy.txt", "r") as f:        return loads(f.read())proxy = get_proxy_from_file()
Enter fullscreen modeExit fullscreen mode

The proxy can now be used with requests:

import requestsresponse = requests.get("url-to-fetch", proxies=proxy)
Enter fullscreen modeExit fullscreen mode

Blog post:http://www.kashifaziz.me/proxy-server-rotation-python.html/

GitHub:https://github.com/kashaziz/rotating-proxy-python

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Solving real world problems with Python. Interested in Business Process Automation, Data Scraping and Product Building | Blogger since 2005
  • Location
    Karachi, Pakistan
  • Joined

More fromKashif Aziz

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp