5 from cStringIO import StringIO
9 # a pycurl-based replacement for the previous version that relied on forking curl
11 def retrieve(url, cacert=None, postdata=None, timeout=90):
13 curl.setopt(pycurl.URL,url)
15 # reproduce --fail from the previous version
16 curl.setopt(pycurl.FAILONERROR,1)
17 # don't want curl sending any signals
18 curl.setopt(pycurl.NOSIGNAL, 1)
20 # do not follow location when attempting to download a file
21 # curl.setopt(pycurl.FOLLOWLOCATION, 0)
23 # store result on the fly
25 curl.setopt(pycurl.WRITEFUNCTION,buffer.write)
29 curl.setopt(pycurl.CONNECTTIMEOUT, timeout)
30 curl.setopt(pycurl.TIMEOUT, timeout)
34 curl.setopt(pycurl.CAINFO, cacert)
35 curl.setopt(pycurl.SSL_VERIFYPEER, 2)
37 curl.setopt(pycurl.SSL_VERIFYPEER, 0)
41 if isinstance(postdata,dict):
42 postfields = urllib.urlencode(postdata)
45 curl.setopt(pycurl.POSTFIELDS, postfields)
51 errcode = curl.getinfo(pycurl.HTTP_CODE)
54 # check the code, return 1 if successfull
56 raise xmlrpclib.ProtocolError (url,errcode, "SSL certificate validation failed", postdata)
58 raise xmlrpclib.ProtocolError (url,errcode, "http error %d"%errcode, postdata)
60 except pycurl.error, err:
62 raise xmlrpclib.ProtocolError(url, errno, "curl error %d: '%s'\n" %(errno,errstr),postdata )
64 return buffer.getvalue()