Quantcast

Jump to content


Photo

Using multiple sock5 in Python with PySocks


  • Please log in to reply
2 replies to this topic

#1 shrouded

shrouded
  • lil'cluck

  • 1250 posts


Users Awards

Posted 07 March 2016 - 11:54 AM

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 :(



#2 data

data
  • 63 posts

Posted 09 June 2016 - 08:25 AM

If you are going to us a third party library then I would suggest looking into python requests.  It is amazingly easy to use amd has proxy support built in.


Edited by data, 09 June 2016 - 08:25 AM.


#3 shrouded

shrouded
  • lil'cluck

  • 1250 posts


Users Awards

Posted 27 June 2016 - 07:16 AM

If you are going to us a third party library then I would suggest looking into python requests.  It is amazingly easy to use amd has proxy support built in.

 

The glitch/exploit I was using this for was verified and subsequently patched by the vendor. I used urllib because I could find previously used code online. Why build something new when you can use something old?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users