CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Public Attributes | Static Public Attributes
uploadConditions.HTTP Class Reference
Inheritance diagram for uploadConditions.HTTP:

Public Member Functions

def __init__
 
def discardCookies
 
def getCookies
 
def getToken
 
def query
 
def setBaseUrl
 
def setProxy
 
def setRetries
 
def setTimeout
 

Public Attributes

 baseUrl
 
 curl
 
 retries
 
 token
 

Static Public Attributes

tuple retryCodes = frozenset([502, 503])
 

Detailed Description

Class used for querying URLs using the HTTP protocol.

Definition at line 223 of file uploadConditions.py.

Constructor & Destructor Documentation

def uploadConditions.HTTP.__init__ (   self)

Definition at line 229 of file uploadConditions.py.

References upload_popcon.HTTP.setBaseUrl(), uploadConditions.HTTP.setBaseUrl(), upload_popcon.HTTP.setRetries(), and uploadConditions.HTTP.setRetries().

230  def __init__(self):
231  self.setBaseUrl()
232  self.setRetries()
234  self.curl = pycurl.Curl()
235  self.curl.setopt(self.curl.COOKIEFILE, '') # in memory
236 
237  #-toDo: make sure we have the right options set here to use ssl
238  #-review(2015-09-25): check and see - action: AP
239  # self.curl.setopt(self.curl.SSL_VERIFYPEER, 1)
240  self.curl.setopt(self.curl.SSL_VERIFYPEER, 0)
241  self.curl.setopt(self.curl.SSL_VERIFYHOST, 2)
243  self.baseUrl = None
245  self.token = None

Member Function Documentation

def uploadConditions.HTTP.discardCookies (   self)
Discards cookies.

Definition at line 251 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.query().

252  def discardCookies(self):
253  '''Discards cookies.
254  '''
255  self.curl.setopt(self.curl.COOKIELIST, 'ALL')
256 
def uploadConditions.HTTP.getCookies (   self)
Returns the list of cookies.

Definition at line 246 of file uploadConditions.py.

247  def getCookies(self):
248  '''Returns the list of cookies.
249  '''
250  return self.curl.getinfo(self.curl.INFO_COOKIELIST)
def uploadConditions.HTTP.getToken (   self,
  username,
  password 
)

Definition at line 291 of file uploadConditions.py.

References upload_popcon.HTTP.baseUrl, uploadConditions.HTTP.baseUrl, cond::TagInfo_t.token, upload_popcon.HTTP.token, uploadConditions.HTTP.token, and upload_popcon.ConditionsUploader.token.

292  def getToken(self, username, password):
293 
294  url = self.baseUrl + 'token'
295 
296  self.curl.setopt(pycurl.URL, url)
297  self.curl.setopt(pycurl.VERBOSE, 0)
298 
299  #-toDo: check if/why these are needed ...
300  #-ap: hmm ...
301  # self.curl.setopt(pycurl.DNS_CACHE_TIMEOUT, 0)
302  # self.curl.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4)
303  #-end hmmm ...
304  #-review(2015-09-25): check and see - action: AP
305 
306 
307  self.curl.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
308  # self.curl.setopt( self.curl.POST, {})
309  self.curl.setopt(self.curl.HTTPGET, 0)
310 
311  response = cStringIO.StringIO()
312  self.curl.setopt(pycurl.WRITEFUNCTION, response.write)
313  self.curl.setopt(pycurl.USERPWD, '%s:%s' % (username, password) )
314 
315  logging.debug('going to connect to server at: %s' % url )
316 
317  self.curl.perform()
318  code = self.curl.getinfo(pycurl.RESPONSE_CODE)
319  logging.debug('got: %s ', str(code))
320 
321  try:
322  self.token = json.loads( response.getvalue() )['token']
323  except Exception as e:
324  logging.error('http::getToken> got error from server: %s ', str(e) )
325  if 'No JSON object could be decoded' in str(e):
326  return None
327  logging.error("error getting token: %s", str(e))
328  return None
329 
330  logging.debug('token: %s', self.token)
331  logging.debug('returning: %s', response.getvalue())
332 
333  return response.getvalue()
def uploadConditions.HTTP.query (   self,
  url,
  data = None,
  files = None,
  keepCookies = True 
)
Queries a URL, optionally with some data (dictionary).

If no data is specified, a GET request will be used.
If some data is specified, a POST request will be used.

If files is specified, it must be a dictionary like data but
the values are filenames.

By default, cookies are kept in-between requests.

A HTTPError exception is raised if the response's HTTP code is not 200.

Definition at line 334 of file uploadConditions.py.

References upload_popcon.HTTP.baseUrl, uploadConditions.HTTP.baseUrl, upload_popcon.HTTP.discardCookies(), uploadConditions.HTTP.discardCookies(), list(), upload_popcon.HTTP.retries, uploadConditions.HTTP.retries, upload_popcon.HTTP.retryCodes, uploadConditions.HTTP.retryCodes, cond::TagInfo_t.token, upload_popcon.HTTP.token, uploadConditions.HTTP.token, and upload_popcon.ConditionsUploader.token.

Referenced by production_tasks.BaseDataset.run(), and edmIntegrityCheck.IntegrityCheck.test().

335  def query(self, url, data = None, files = None, keepCookies = True):
336  '''Queries a URL, optionally with some data (dictionary).
337 
338  If no data is specified, a GET request will be used.
339  If some data is specified, a POST request will be used.
340 
341  If files is specified, it must be a dictionary like data but
342  the values are filenames.
343 
344  By default, cookies are kept in-between requests.
345 
346  A HTTPError exception is raised if the response's HTTP code is not 200.
347  '''
348 
349  if not keepCookies:
350  self.discardCookies()
351 
352  url = self.baseUrl + url
353 
354  # make sure the logs are safe ... at least somewhat :)
355  data4log = copy.copy(data)
356  if data4log:
357  if 'password' in data4log.keys():
358  data4log['password'] = '*'
359 
360  retries = [0] + list(self.retries)
361 
362  while True:
363  logging.debug('Querying %s with data %s and files %s (retries left: %s, current sleep: %s)...', url, data4log, files, len(retries), retries[0])
364 
365  time.sleep(retries.pop(0))
366 
367  try:
368  self.curl.setopt(self.curl.URL, url)
369  self.curl.setopt(self.curl.HTTPGET, 1)
370 
371  # from now on we use the token we got from the login
372  self.curl.setopt(pycurl.USERPWD, '%s:""' % ( str(self.token), ) )
373  self.curl.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
374 
375  if data is not None or files is not None:
376  # If there is data or files to send, use a POST request
377 
378  finalData = {}
379 
380  if data is not None:
381  finalData.update(data)
382 
383  if files is not None:
384  for (key, fileName) in files.items():
385  finalData[key] = (self.curl.FORM_FILE, fileName)
386  self.curl.setopt( self.curl.HTTPPOST, finalData.items() )
387 
388  self.curl.setopt(pycurl.VERBOSE, 0)
389 
390  response = cStringIO.StringIO()
391  self.curl.setopt(self.curl.WRITEFUNCTION, response.write)
392  self.curl.perform()
393 
394  code = self.curl.getinfo(self.curl.RESPONSE_CODE)
395 
396  if code in self.retryCodes and len(retries) > 0:
397  logging.debug('Retrying since we got the %s error code...', code)
398  continue
399 
400  if code != 200:
401  raise HTTPError(code, response.getvalue())
402 
403  return response.getvalue()
404 
405  except pycurl.error as e:
406  if len(retries) == 0:
407  raise e
408  logging.debug('Retrying since we got the %s pycurl exception...', str(e))
409 
410 # common/http.py end
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run
def uploadConditions.HTTP.setBaseUrl (   self,
  baseUrl = '' 
)
Allows to set a base URL which will be prefixed to all the URLs
that will be queried later.

Definition at line 257 of file uploadConditions.py.

References upload_popcon.HTTP.baseUrl, and uploadConditions.HTTP.baseUrl.

Referenced by uploadConditions.HTTP.__init__().

258  def setBaseUrl(self, baseUrl = ''):
259  '''Allows to set a base URL which will be prefixed to all the URLs
260  that will be queried later.
261  '''
262  self.baseUrl = baseUrl
263 
def uploadConditions.HTTP.setProxy (   self,
  proxy = '' 
)
Allows to set a proxy.

Definition at line 264 of file uploadConditions.py.

265  def setProxy(self, proxy = ''):
266  '''Allows to set a proxy.
267  '''
268  self.curl.setopt(self.curl.PROXY, proxy)
269 
def uploadConditions.HTTP.setRetries (   self,
  retries = () 
)
Allows to set retries.

The retries are a sequence of the seconds to wait per retry.

The retries are done on:
    * PyCurl errors (includes network problems, e.g. not being able
      to connect to the host).
    * 502 Bad Gateway (for the moment, to avoid temporary
      Apache-CherryPy issues).
    * 503 Service Temporarily Unavailable (for when we update
      the frontends).

Definition at line 276 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.__init__().

277  def setRetries(self, retries = ()):
278  '''Allows to set retries.
279 
280  The retries are a sequence of the seconds to wait per retry.
281 
282  The retries are done on:
283  * PyCurl errors (includes network problems, e.g. not being able
284  to connect to the host).
285  * 502 Bad Gateway (for the moment, to avoid temporary
286  Apache-CherryPy issues).
287  * 503 Service Temporarily Unavailable (for when we update
288  the frontends).
289  '''
290  self.retries = retries
def uploadConditions.HTTP.setTimeout (   self,
  timeout = 0 
)
Allows to set a timeout.

Definition at line 270 of file uploadConditions.py.

271  def setTimeout(self, timeout = 0):
272  '''Allows to set a timeout.
273  '''
274  self.curl.setopt(self.curl.TIMEOUT, timeout)
275 

Member Data Documentation

uploadConditions.HTTP.baseUrl

Definition at line 242 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.getToken(), uploadConditions.HTTP.query(), and uploadConditions.HTTP.setBaseUrl().

uploadConditions.HTTP.curl

Definition at line 233 of file uploadConditions.py.

uploadConditions.HTTP.retries

Definition at line 289 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.query().

tuple uploadConditions.HTTP.retryCodes = frozenset([502, 503])
static

Definition at line 227 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.query().

uploadConditions.HTTP.token

Definition at line 244 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.getToken(), uploadConditions.HTTP.query(), and uploadConditions.ConditionsUploader.signOut().