DEV Community

Momen Fathi
Momen Fathi

Posted on

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

Top comments (0)