我有一小段代碼,如下圖所示,使用的urllib2 ..我試圖將其轉換爲pycurl從pycurl代理支持中獲益。 pycurl的轉換後的代碼顯示在原始代碼之後..我想知道如何將urllib.urlopen(req).read()更改爲pycurl中類似的東西..也許使用類似strictIO的東西?從urllib2的遷移到pycurl
的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))
在此先感謝。
@abarnert謝謝..我會編輯這個問題。 – tkyass