DEV Community

Momen Fathi
Momen Fathi

Posted on

3 3

Reading Proxy List To Fit Mechanize.Browser()

!/usr/bin/python

import random
import yaml

banner = """
Reading Proxy List from File
Formated -> Socks(4-5)\HTTP\HTTPS:IP:PORT

Example :
cat proxylist.txt

http:1.1.1.1:8080
https:2.2.2.2:9090

"""
print(banner)

proxyfilepath = str(raw_input(" Enter proxy List Path,
with Content format as Socks(4-5)\HTTP\HTTPS:IP:PORT) Here : "))

proxy_dict = {}
x = open(proxyfilepath,'r').readlines()

plines = len(x)
print " Number of Proxies in File are : %s " % plines

proxy_num = 0
for line in x:
proxy_num += 1
proxy_type, proxy_server, proxy_port = line.strip("\n").split(":")
proxy_dict[proxy_num]= "{"+"'"+proxy_type+"'" + ': ' + "'"+proxy_server + ':' + proxy_port+"'"+"}"

print "\n Showing the Proxy lines in Dict format stored in Proxy_dict \n"
print proxy_dict

counter = 1

aplst= []

while counter <= plines:
sign = proxy_dict[(counter)]
counter += 1
aplst.append(sign)

print "\n\n Done Signing\n\n"
print " Showing The List of Dictionary format proxies \n"
print aplst

print "\n\n Showing Random Choice from The List and It's Data Type .. \n"
print (random.choice(aplst)) , type(random.choice(aplst))
print
print "\n So we Should be Converting the Token Proxy using (yaml) to Map correctly through the protocol \n"
converted = yaml.load((random.choice(aplst)))
print converted , type(converted)


-And this command (random.choice(aplst)) can be used inside
the Mechanize.Browser() proxy option after Converting
The "proxy" token from the list as a string to "Dict" Using (yaml) as Example:

pr_token = yaml.load((random.choice(aplst)))
br = mechanize.Browser()
br.set_proxies(pr_token)

LyJoker Greetz to Da.Master

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️