Last active
April 7, 2023 15:27
-
-
Save sp3c73r2038/4159510 to your computer and use it in GitHub Desktop.
urlopen with proxy support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple example, and it perfectly helped me understand how the ProxyHandler is implemented. You rock