Skip to content

Instantly share code, notes, and snippets.

@sp3c73r2038
Last active April 7, 2023 15:27
Show Gist options
  • Save sp3c73r2038/4159510 to your computer and use it in GitHub Desktop.
Save sp3c73r2038/4159510 to your computer and use it in GitHub Desktop.
urlopen with proxy support
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
import urllib2
data = urllib2.urlencode({'foo': 'bar'})
r = urllib2.Request('https://www.google.com', # url
data, # post data
{'Accept': 'application/json'}) # header
res = urllib2.urlopen(r) # performing request
print res.read() # retrieve response
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
proxy_support = urllib2.ProxyHandler({"http": "http://192.168.2.20:8123",
"https": "http://192.168.2.20:8123"})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
# f = urllib2.urlopen("http://www.google.com")
f = urllib2.urlopen("http://twitter.com/")
print f.read()
@MatchTamlinsn
Copy link

A simple example, and it perfectly helped me understand how the ProxyHandler is implemented. You rock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment