CMS 3D CMS Logo

url_query.py
Go to the documentation of this file.
1 """
2 Joshua Dawes - CERN, CMS, The University of Manchester
3 
4 File provides a class that handles pycurl requests.
5 
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
8 """
9 from __future__ import print_function
10 
11 import pycurl
12 from StringIO import StringIO
13 from urllib import urlencode
14 import traceback
15 import sys
16 import json
17 from errors import *
18 from time import sleep
19 
20 class url_query():
21 
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 
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:66
def send(self)
Definition: url_query.py:65
def __init__(self, url=None, url_data=None, body=None, response_stream=None, timeout=60)
Definition: url_query.py:22
#define str(s)