https://github.com/Anorov/PySocks
import urllib2 import socks from sockshandler import SocksiPyHandler opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 9050)) print opener.open("http://www.somesite.com/") # All requests made by the opener will pass through the SOCKS proxy
I need to be able to navigate a few pages and submit POST requests. From there I want to be able to close the socket and open another with a different socks5 IP/Port. From what I've read online it seems like the handler above is the solution I want. I am concerned looping through a bunch of urllib2.build_opener() would leave me with hundreds of handlers. This isn't my area of expertise to say the least.. The entire idea is below:
-Open handler with sock5
-Open web page
-POST data
-End Session
-Repeat
Can I just redefine opener ? Make the IP/Port go down a list of combinations from an array. Bad code below showing what I mean sort of..
import urllib2 import socks from sockshandler import SocksiPyHandler opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, "SOCK5_IP_LIST[1]", SOCK5_PORT_LSIT[1])) print opener.open("https://internet.yandex.com/get_full_info/") # All requests made by the opener will pass through the SOCKS proxy opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, "SOCK5_IP_LIST[2]", SOCK5_PORT_LSIT[2])) print opener.open("https://internet.yandex.com/get_full_info/") # All requests made by the opener will pass through the SOCKS proxy
Will this result in Yandex showing the first sock5 IP in the first print and the second sock5 IP in the second print?
I have been trying to find example source code using any socks handler, but with no success. I haven't spent too much time looking because I am at work, but hopefully once I leave I will be able to find what I'm looking for.
import urllib2 import socks import cookielib from sockshandler import SocksiPyHandler import cookielib, urllib2 sock5 = ['xxx.xxx.xxx', 'xxx.xxx.xxx', 'xxx.xxx.xxx'] port = ['port', 'port', 'port'] for index in range(len(sock5)): cj = cookielib.CookieJar() opener = urllib2.build_opener( SocksiPyHandler(socks.SOCKS5, "sock5[index]", port[index]), urllib2.HTTPCookieProcessor(cj)) print opener.open("https://internet.yandex.com/get_full_info/")
Another quickly put together piece of code. This is the solution according to @Pyro666. If you include the cookiejar inside of the loop it will effectively remove all previous cookies. Each loop will be an independent "browser" under a different IP. You can branch out from here and add in a random user-agent/referrer in the same way. The above doesn't include referrer/user-agent, but it should be possible to add it to the opener with no issue.
Ex.
A matrix of IP|Port|Header|Referrer with each row containing 1 sock5, 1 user-agent, and 1 referrer for the initial load. This isn't the ideal set up, but it would work. It would be better to have XXX.XXX.XXX.XXX:PORT as a single column or completely separately. This way you could grab a random row from the header/referrer list or even have arrays of different lengths. Top 200 user-agents, but only 30-50 referrers. Depends on your personal situation.
Once I write up the final code I'll post it. Expect to be waiting 1-5 days. Busy life