2 Joshua Dawes - CERN, CMS, The University of Manchester 4 File provides a class that handles pycurl requests. 6 Provides methods for performing/closing the request, as well as getting the request response. 7 Note: user agent string from current version of cmsDbUpload 12 from io
import StringIO
13 from urllib.parse
import urlencode
18 from time
import sleep
21 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
25 def __init__(self, url=None, url_data=None, body=None):
32 return requests.post(self.
_url, data=self.
_body, params=self.
_url_data, verify=
False).text
34 return requests.get(self.
_url, params=self.
_url_data, verify=
False).text
38 def __init__(self, url=None, url_data=None, body=None, response_stream=None, timeout=60): 42 self._r = pycurl.Curl() 44 # set options for the request - of note is the fact that we do not verify the peer or the host - because 45 # CERN certificates are self-signed, and we only need the encryption from HTTPS, not the certificate checks. 47 self._r.setopt(self._r.CONNECTTIMEOUT, timeout) 48 user_agent = "User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" % (sys.version_info[ :3 ] + (pycurl.version_info()[1],)) 49 self._r.setopt(self._r.USERAGENT, user_agent) 50 # we don't need to verify who signed the certificate or who the host is 51 self._r.setopt(self._r.SSL_VERIFYPEER, 0) 52 self._r.setopt(self._r.SSL_VERIFYHOST, 0) 53 self._response = StringIO() 56 if type(body) == dict: 57 body = urlencode(body) 58 elif type(body) == list: 59 body = json.dumps(body) 61 self._r.setopt(self._r.POSTFIELDS, body) 64 if type(url_data) == dict: 65 url_data = urlencode(url_data) 67 exit("URL data '%s' for request to URL '%s' was not valid - should be a dictionary." % (str(url_data), url)) 69 # set the URL with url parameters if they were given 70 self._r.setopt(self._r.URL, url + (("?%s" % url_data) if url_data else "")) 72 if response_stream and type(response_stream) != StringIO: 73 response_stream = StringIO() 74 # copy reference to instance variable 75 self._response = response_stream 76 elif not(response_stream): 77 self._response = StringIO() 79 self._r.setopt(self._r.WRITEFUNCTION, self._response.write) 85 # retry while we're within the limit for the number of retries 86 while failed and attempt < max_retries: 91 return self._response.getvalue() 92 except Exception as e: 95 # this catches exceptions that occur with the actual http request 96 # not exceptions sent back from server side 97 if type(e) == pycurl.error and e[0] in [7, 52]: 98 # wait two seconds to retry 99 print("Request failed - waiting 3 seconds to retry.") 104 print("Unforesoon error occurred when sending data to server.") 105 traceback.print_exc() 106 if attempt == max_retries: 107 raise NoMoreRetriesException(max_retries)"""
def __init__(self, url=None, url_data=None, body=None)