我有一段代码,如下所示,它使用 urllib2 .. 我正在尝试将其转换为 pycurl 以受益于 pycurl 代理支持。pycurl 的转换代码显示在原始代码之后。我想知道如何将 urllib.urlopen(req).read() 更改为 pycurl 中的类似内容。也许使用类似 strinIO 的东西?
urllib2 代码:
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))
具有代理支持的 pycurl 转换:
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))
提前致谢。