Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:54

0001 """
0002 Joshua Dawes - CERN, CMS, The University of Manchester
0003 
0004 File provides a class that handles pycurl requests.
0005 
0006 Provides methods for performing/closing the request, as well as getting the request response.
0007 Note: user agent string from current version of cmsDbUpload
0008 """
0009 
0010 #import pycurl
0011 import requests
0012 from io import StringIO
0013 from urllib.parse import urlencode
0014 import traceback
0015 import sys
0016 import json
0017 from .errors import *
0018 from time import sleep
0019 
0020 import urllib3
0021 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
0022 
0023 class url_query():
0024 
0025     def __init__(self, url=None, url_data=None, body=None):
0026         self._url = url
0027         self._url_data = url_data
0028         self._body = body
0029 
0030     def send(self):
0031         if self._body:
0032             return requests.post(self._url, data=self._body, params=self._url_data, verify=False).text
0033         else:
0034             return requests.get(self._url, params=self._url_data, verify=False).text
0035 
0036 """class url_query():
0037 
0038     def __init__(self, url=None, url_data=None, body=None, response_stream=None, timeout=60):
0039         if not(url):
0040             return None
0041         self._url = url
0042         self._r = pycurl.Curl()
0043 
0044         # set options for the request - of note is the fact that we do not verify the peer or the host - because
0045         # CERN certificates are self-signed, and we only need the encryption from HTTPS, not the certificate checks.
0046 
0047         self._r.setopt(self._r.CONNECTTIMEOUT, timeout)
0048         user_agent = "User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" % (sys.version_info[ :3 ] + (pycurl.version_info()[1],))
0049         self._r.setopt(self._r.USERAGENT, user_agent)
0050         # we don't need to verify who signed the certificate or who the host is
0051         self._r.setopt(self._r.SSL_VERIFYPEER, 0)
0052         self._r.setopt(self._r.SSL_VERIFYHOST, 0)
0053         self._response = StringIO()
0054 
0055         if body:
0056             if type(body) == dict:
0057                 body = urlencode(body)
0058             elif type(body) == list:
0059                 body = json.dumps(body)
0060 
0061             self._r.setopt(self._r.POSTFIELDS, body)
0062 
0063         if url_data:
0064             if type(url_data) == dict:
0065                 url_data = urlencode(url_data)
0066             else:
0067                 exit("URL data '%s' for request to URL '%s' was not valid - should be a dictionary." % (str(url_data), url))
0068 
0069         # set the URL with url parameters if they were given
0070         self._r.setopt(self._r.URL, url + (("?%s" % url_data) if url_data else ""))
0071 
0072         if response_stream and type(response_stream) != StringIO:
0073             response_stream = StringIO()
0074             # copy reference to instance variable
0075             self._response = response_stream
0076         elif not(response_stream):
0077             self._response = StringIO()
0078 
0079         self._r.setopt(self._r.WRITEFUNCTION, self._response.write)
0080 
0081     def send(self):
0082         failed = True
0083         max_retries = 5
0084         attempt = 0
0085         # retry while we're within the limit for the number of retries
0086         while failed and attempt < max_retries:
0087             try:
0088                 self._r.perform()
0089                 failed = False
0090                 self._r.close()
0091                 return self._response.getvalue()
0092             except Exception as e:
0093                 failed = True
0094                 attempt += 1
0095                 # this catches exceptions that occur with the actual http request
0096                 # not exceptions sent back from server side
0097                 if type(e) == pycurl.error and e[0] in [7, 52]:
0098                     # wait two seconds to retry
0099                     print("Request failed - waiting 3 seconds to retry.")
0100                     sleep(3)
0101                     # do nothing for now
0102                     pass
0103                 else:
0104                     print("Unforesoon error occurred when sending data to server.")
0105                     traceback.print_exc()
0106         if attempt == max_retries:
0107             raise NoMoreRetriesException(max_retries)"""