2 Joshua Dawes - CERN, CMS, The University of Manchester 4 File provides a class that handles pycurl requests. 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 9 from __future__
import print_function
10 from __future__
import absolute_import
13 from StringIO
import StringIO
14 from urllib
import urlencode
19 from time
import sleep
23 def __init__(self, url=None, url_data=None, body=None, response_stream=None, timeout=60):
27 self.
_r = pycurl.Curl()
32 self._r.setopt(self._r.CONNECTTIMEOUT, timeout)
33 user_agent =
"User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" % (sys.version_info[ :3 ] + (pycurl.version_info()[1],))
34 self._r.setopt(self._r.USERAGENT, user_agent)
36 self._r.setopt(self._r.SSL_VERIFYPEER, 0)
37 self._r.setopt(self._r.SSL_VERIFYHOST, 0)
41 if isinstance(body, dict):
42 body = urlencode(body)
43 elif isinstance(body, list):
44 body = json.dumps(body)
46 self._r.setopt(self._r.POSTFIELDS, body)
49 if isinstance(url_data, dict):
50 url_data = urlencode(url_data)
52 exit(
"URL data '%s' for request to URL '%s' was not valid - should be a dictionary." % (
str(url_data), url))
55 self._r.setopt(self._r.URL, url + ((
"?%s" % url_data)
if url_data
else ""))
57 if response_stream
and not isinstance(response_stream, StringIO):
58 response_stream = StringIO()
61 elif not(response_stream):
64 self._r.setopt(self._r.WRITEFUNCTION, self._response.write)
71 while failed
and attempt < max_retries:
76 return self._response.getvalue()
77 except Exception
as e:
82 if isinstance(e, pycurl.error)
and e[0]
in [7, 52]:
84 print(
"Request failed - waiting 3 seconds to retry.")
89 print(
"Unforesoon error occurred when sending data to server.")
91 if attempt == max_retries:
S & print(S &os, JobReport::InputFile const &f)
def __init__(self, url=None, url_data=None, body=None, response_stream=None, timeout=60)