stan
stan

Reputation: 4995

Removing SOCKS 4/5 proxy

This question is sort of the opposite of this:

How can I use a SOCKS 4/5 proxy with urllib2?

Let's say I use a SOCKS 5 proxy using the method accepted in that question. How would I revert it back to no proxy in the same process?

i.e start process use proxy .. remove proxy ...

Maybe there is a better way to use the proxy so that it's easier to remove it later?

Upvotes: 3

Views: 2622

Answers (1)

Robus
Robus

Reputation: 8259

Abra kadabra

import socks,socket,urllib2
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 8080)
temp = socket.socket
socket.socket = socks.socksocket  
print urllib2.urlopen('http://www.google.com').read() // Proxy
socket.socket=temp
print urllib2.urlopen('http://www.google.com').read() // No proxy

Upvotes: 11

Related Questions