Created
December 11, 2013 08:33
-
-
Save exhuma/7906879 to your computer and use it in GitHub Desktop.
Python urllib HTTPS request with proxies
This file contains 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
HTTP_PROXY = { | |
"http": 'http://proxy.host:8888', | |
"https": 'http://proxy.host:8888' | |
} | |
def create(self, stanza, passwd): | |
''' | |
send xml data to ripe | |
return True on succes or HTTPError on error | |
''' | |
import urllib2 | |
import urllib | |
payload = stanza.toxml() | |
headers = {'content-type': 'application/xml'} | |
params = {'password': passwd} | |
proxy = urllib2.ProxyHandler(HTTP_PROXY) | |
opener = urllib2.build_opener(proxy) | |
urllib2.install_opener(opener) | |
url = 'https://apps.db.ripe.net/whois/create?{0}'.format( | |
urllib.urlencode(params) | |
) | |
r = urllib2.Request(url, payload, headers) | |
try: | |
response = urllib2.urlopen(r) | |
response_header = response.info() | |
self.code = response.getcode() | |
self.header = response_header | |
self.payload = payload | |
return self.code, self.header, self.payload | |
except urllib2.HTTPError as exc: | |
LOG.warning("Commit Error {0} URL was : {1}".format(exc.code, url)) | |
return exc.code, exc.read(), payload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment