views:

72

answers:

2

This question is sort of the opposite of this:

http://stackoverflow.com/questions/2317849/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?

A: 

“Have you tried plugging it in?”

+5  A: 

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
Robus
Actually, you should restore socket.socket=temp in a "finally" clause.
DomQ