Reputation: 1596
Hi I need to do multiple parallel request each from different IP, so I have two instances of tor
initiated using stem
module. Lets say one is running at 127.0.0.1:9150
and second on 127.0.0.1:9050
.
Then if I want to use the proxy I do
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 9050)
socket.socket = socks.socksocket
and check my IP by
resp = requests.get('https://api.ipify.org?format=json', timeout=5)
j = json.loads(resp.content)
print('New IP: '+ j['ip'])
This works for a single proxy, but can I somehow specify which of the two proxies should the requests.get()
method use? Probably somehow limit the scope of the first part of the code?
Thanks
Upvotes: 0
Views: 535
Reputation: 312650
Rather than monkey-patching the sockets
module, here's an alternative solution.
The requests
module already has support for using http proxies, as described here; for example:
import requests
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080',
}
requests.get('http://example.org', proxies=proxies)
Of course, you want to use a SOCKS proxy, for which there is no
native support. However, the
polipo
http proxy knows how to speak to an upstream SOCKS proxy, and can thus
be used to translate between what the requests
modules supports and
your upstream tor proxies.
In addition to your two tor proxies, you would run two polipo proxies,
and then point requests at one or the other using the proxies
option
to the various requests
methods.
Your polipo configurations would look something like this:
socksParentProxy = localhost:9050
socksProxyType = socks5
This would work without requiring patches to either the sockets
module or the requests
module.
Upvotes: 2