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 
10 #import pycurl
11 import requests
12 from io import StringIO
13 from urllib.parse import urlencode
14 import traceback
15 import sys
16 import json
17 from .errors import *
18 from time import sleep
19 
20 import urllib3
21 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
22 
23 class url_query():
24 
25  def __init__(self, url=None, url_data=None, body=None):
26  self._url = url
27  self._url_data = url_data
28  self._body = body
29 
30  def send(self):
31  if self._body:
32  return requests.post(self._url, data=self._body, params=self._url_data, verify=False).text
33  else:
34  return requests.get(self._url, params=self._url_data, verify=False).text
35 
36 """class url_query():
37 
38  def __init__(self, url=None, url_data=None, body=None, response_stream=None, timeout=60):
39  if not(url):
40  return None
41  self._url = url
42  self._r = pycurl.Curl()
43 
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.
46 
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()
54 
55  if body:
56  if type(body) == dict:
57  body = urlencode(body)
58  elif type(body) == list:
59  body = json.dumps(body)
60 
61  self._r.setopt(self._r.POSTFIELDS, body)
62 
63  if url_data:
64  if type(url_data) == dict:
65  url_data = urlencode(url_data)
66  else:
67  exit("URL data '%s' for request to URL '%s' was not valid - should be a dictionary." % (str(url_data), url))
68 
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 ""))
71 
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()
78 
79  self._r.setopt(self._r.WRITEFUNCTION, self._response.write)
80 
81  def send(self):
82  failed = True
83  max_retries = 5
84  attempt = 0
85  # retry while we're within the limit for the number of retries
86  while failed and attempt < max_retries:
87  try:
88  self._r.perform()
89  failed = False
90  self._r.close()
91  return self._response.getvalue()
92  except Exception as e:
93  failed = True
94  attempt += 1
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.")
100  sleep(3)
101  # do nothing for now
102  pass
103  else:
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)
Definition: url_query.py:25
def send(self)
Definition: url_query.py:30