CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
url_query.url_query Class Reference

Public Member Functions

def __init__ (self, url=None, url_data=None, body=None, response_stream=None, timeout=60)
 
def send (self)
 

Private Attributes

 _r
 
 _response
 
 _url
 

Detailed Description

Definition at line 20 of file url_query.py.

Constructor & Destructor Documentation

def url_query.url_query.__init__ (   self,
  url = None,
  url_data = None,
  body = None,
  response_stream = None,
  timeout = 60 
)

Definition at line 22 of file url_query.py.

22  def __init__(self, url=None, url_data=None, body=None, response_stream=None, timeout=60):
23  if not(url):
24  return None
25  self._url = url
26  self._r = pycurl.Curl()
27 
28  # set options for the request - of note is the fact that we do not verify the peer or the host - because
29  # CERN certificates are self-signed, and we only need the encryption from HTTPS, not the certificate checks.
30 
31  self._r.setopt(self._r.CONNECTTIMEOUT, timeout)
32  user_agent = "User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" % (sys.version_info[ :3 ] + (pycurl.version_info()[1],))
33  self._r.setopt(self._r.USERAGENT, user_agent)
34  # we don't need to verify who signed the certificate or who the host is
35  self._r.setopt(self._r.SSL_VERIFYPEER, 0)
36  self._r.setopt(self._r.SSL_VERIFYHOST, 0)
37  self._response = StringIO()
38 
39  if body:
40  if isinstance(body, dict):
41  body = urlencode(body)
42  elif isinstance(body, list):
43  body = json.dumps(body)
44 
45  self._r.setopt(self._r.POSTFIELDS, body)
46 
47  if url_data:
48  if isinstance(url_data, dict):
49  url_data = urlencode(url_data)
50  else:
51  exit("URL data '%s' for request to URL '%s' was not valid - should be a dictionary." % (str(url_data), url))
52 
53  # set the URL with url parameters if they were given
54  self._r.setopt(self._r.URL, url + (("?%s" % url_data) if url_data else ""))
55 
56  if response_stream and not isinstance(response_stream, StringIO):
57  response_stream = StringIO()
58  # copy reference to instance variable
59  self._response = response_stream
60  elif not(response_stream):
61  self._response = StringIO()
62 
63  self._r.setopt(self._r.WRITEFUNCTION, self._response.write)
64 
def __init__(self, url=None, url_data=None, body=None, response_stream=None, timeout=60)
Definition: url_query.py:22
#define str(s)

Member Function Documentation

def url_query.url_query.send (   self)

Definition at line 65 of file url_query.py.

References edm.print().

65  def send(self):
66  failed = True
67  max_retries = 5
68  attempt = 0
69  # retry while we're within the limit for the number of retries
70  while failed and attempt < max_retries:
71  try:
72  self._r.perform()
73  failed = False
74  self._r.close()
75  return self._response.getvalue()
76  except Exception as e:
77  failed = True
78  attempt += 1
79  # this catches exceptions that occur with the actual http request
80  # not exceptions sent back from server side
81  if isinstance(e, pycurl.error) and e[0] in [7, 52]:
82  # wait two seconds to retry
83  print("Request failed - waiting 3 seconds to retry.")
84  sleep(3)
85  # do nothing for now
86  pass
87  else:
88  print("Unforesoon error occurred when sending data to server.")
89  traceback.print_exc()
90  if attempt == max_retries:
91  raise NoMoreRetriesException(max_retries)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def send(self)
Definition: url_query.py:65

Member Data Documentation

url_query.url_query._r
private

Definition at line 26 of file url_query.py.

url_query.url_query._response
private

Definition at line 37 of file url_query.py.

url_query.url_query._url
private

Definition at line 25 of file url_query.py.

Referenced by cmstc.TagCollector._open(), and conddblib.Connection.session().