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 19 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 21 of file url_query.py.

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

Member Function Documentation

def url_query.url_query.send (   self)

Definition at line 64 of file url_query.py.

References reco.print().

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

Member Data Documentation

url_query.url_query._r
private

Definition at line 25 of file url_query.py.

url_query.url_query._response
private

Definition at line 36 of file url_query.py.

url_query.url_query._url
private

Definition at line 24 of file url_query.py.

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