Skip to content

Instantly share code, notes, and snippets.

@exhuma
Created December 11, 2013 08:33
Show Gist options
  • Save exhuma/7906879 to your computer and use it in GitHub Desktop.
Save exhuma/7906879 to your computer and use it in GitHub Desktop.
Python urllib HTTPS request with proxies
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