basics are OK; still needs to prevent the regular slivermanager from starting slivers...
[nodemanager.git] / curlwrapper.py
1 import os
2 import xmlrpclib
3 import urllib
4 import pycurl
5 from cStringIO import StringIO
6
7 import logger
8
9 # a pycurl-based replacement for the previous version that relied on forking curl
10
11 def retrieve(url, cacert=None, postdata=None, timeout=90):
12     curl= pycurl.Curl()
13     curl.setopt(pycurl.URL,url)
14
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)
19
20     # do not follow location when attempting to download a file
21     # curl.setopt(pycurl.FOLLOWLOCATION, 0)
22
23     # store result on the fly 
24     buffer=StringIO()
25     curl.setopt(pycurl.WRITEFUNCTION,buffer.write)
26            
27     # set timeout
28     if timeout: 
29         curl.setopt(pycurl.CONNECTTIMEOUT, timeout)
30         curl.setopt(pycurl.TIMEOUT, timeout)
31
32     # set cacert
33     if cacert: 
34         curl.setopt(pycurl.CAINFO, cacert)
35         curl.setopt(pycurl.SSL_VERIFYPEER, 2)
36     else:
37         curl.setopt(pycurl.SSL_VERIFYPEER, 0)
38
39     # set postdata
40     if postdata:
41         if isinstance(postdata,dict):
42             postfields = urllib.urlencode(postdata)
43         else:
44             postfields=postdata
45         curl.setopt(pycurl.POSTFIELDS, postfields)
46
47     # go
48     try:
49         curl.perform()
50
51         errcode = curl.getinfo(pycurl.HTTP_CODE)
52         curl.close()
53
54         # check the code, return 1 if successfull
55         if errcode == 60:
56             raise xmlrpclib.ProtocolError (url,errcode, "SSL certificate validation failed", postdata)
57         elif errcode != 200:
58             raise xmlrpclib.ProtocolError (url,errcode, "http error %d"%errcode, postdata)
59
60     except pycurl.error, err:
61         errno, errstr = err
62         raise xmlrpclib.ProtocolError(url, errno, "curl error %d: '%s'\n" %(errno,errstr),postdata )
63
64     return buffer.getvalue()