3

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?

Community
  • 1
  • 1
stan
  • 4,885
  • 5
  • 49
  • 72

1 Answers1

11

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
  • 8,067
  • 5
  • 47
  • 67