I have a snippet of code, shown below, that uses urllib2 .. I'm trying to convert it to pycurl to benefit from the pycurl proxy support. The converted code of pycurl is shown after the original code.. I want to know how to change the urllib.urlopen(req).read() to something similar in pycurl .. maybe using something like strinIO?
urllib2 code:
URL = 'URL' UN = 'UN' PWD = 'PWD' HEADERS = { 'Accept': 'application/json', 'Connection': 'Keep-Alive', 'Accept-Encoding' : 'gzip', 'Authorization' : 'Basic %s' % base64.encodestring('%s:%s' % (UN, PWD)) } req = urllib2.Request(URL, headers=HEADERS) response = urllib2.urlopen(req, timeout=(KEEP_ALIVE)) # header - print response.info() decompressor = zlib.decompressobj(16+zlib.MAX_WBITS) remainder = '' while True: tmp = decompressor.decompress(response.read(CHUNKSIZE)) the pycurl conversion with proxy support:
URL = 'URL' UN = 'UN' PWD = 'PWD' HEADERS = [ 'Accept : application/json', 'Connection : Keep-Alive', 'Accept-Encoding : gzip', 'Authorization : Basic %s' % base64.encodestring('%s:%s' % (UN, PWD)) ] req = pycurl.Curl() req.setopt(pycurl.CONNECTTIMEOUT,KEEP_ALIVE) req.setopt(pycurl.HTTPHEADER, HEADERS) req.setopt(pycurl.TIMEOUT, 1+KEEP_ALIVE) req.setopt(pycurl.PROXY, 'http://my-proxy') req.setopt(pycurl.PROXYPORT, 8080) req.setopt(pycurl.PROXYUSERPWD, "proxy_access_user : proxy_access_password") req.setopt(pycurl.URL , URL) response = req.perform() decompressor = zlib.decompressobj(16+zlib.MAX_WBITS) remainder = '' while True: tmp = decompressor.decompress(urllib2.urlopen(req).read(CHUNKSIZE)) thanks in advance.