CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes
uploadConditions.HTTP Class Reference
Inheritance diagram for uploadConditions.HTTP:

Public Member Functions

def __init__ (self)
 
def discardCookies (self)
 
def getCookies (self)
 
def getToken (self, username, password)
 
def query (self, url, data=None, files=None, keepCookies=True)
 
def setBaseUrl (self, baseUrl='')
 
def setProxy (self, proxy='')
 
def setRetries (self, retries=())
 
def setTimeout (self, timeout=0)
 

Public Attributes

 baseUrl
 
 curl
 
 retries
 
 token
 

Detailed Description

Class used for querying URLs using the HTTP protocol.

Definition at line 230 of file uploadConditions.py.

Constructor & Destructor Documentation

def uploadConditions.HTTP.__init__ (   self)

Definition at line 236 of file uploadConditions.py.

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

236  def __init__(self):
237  self.setBaseUrl()
238  self.setRetries()
239 
240  self.curl = pycurl.Curl()
241  self.curl.setopt(self.curl.COOKIEFILE, '') # in memory
242 
243  #-toDo: make sure we have the right options set here to use ssl
244  #-review(2015-09-25): check and see - action: AP
245  # self.curl.setopt(self.curl.SSL_VERIFYPEER, 1)
246  self.curl.setopt(self.curl.SSL_VERIFYPEER, 0)
247  self.curl.setopt(self.curl.SSL_VERIFYHOST, 2)
248 
249  self.baseUrl = None
250 
251  self.token = None
252 
def setRetries(self, retries=())
def setBaseUrl(self, baseUrl='')

Member Function Documentation

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

Definition at line 258 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.query().

258  def discardCookies(self):
259  '''Discards cookies.
260  '''
261  self.curl.setopt(self.curl.COOKIELIST, 'ALL')
262 
263 
def uploadConditions.HTTP.getCookies (   self)
Returns the list of cookies.

Definition at line 253 of file uploadConditions.py.

253  def getCookies(self):
254  '''Returns the list of cookies.
255  '''
256  return self.curl.getinfo(self.curl.INFO_COOKIELIST)
257 
def uploadConditions.HTTP.getToken (   self,
  username,
  password 
)

Definition at line 298 of file uploadConditions.py.

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

298  def getToken(self, username, password):
299 
300  url = self.baseUrl + 'token'
301 
302  self.curl.setopt(pycurl.URL, url)
303  self.curl.setopt(pycurl.VERBOSE, 0)
304 
305  #-toDo: check if/why these are needed ...
306  #-ap: hmm ...
307  # self.curl.setopt(pycurl.DNS_CACHE_TIMEOUT, 0)
308  # self.curl.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4)
309  #-end hmmm ...
310  #-review(2015-09-25): check and see - action: AP
311 
312  self.curl.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
313  # self.curl.setopt( self.curl.POST, {})
314  self.curl.setopt(self.curl.HTTPGET, 0)
315 
316  response = cStringIO.StringIO()
317  self.curl.setopt(pycurl.WRITEFUNCTION, response.write)
318  self.curl.setopt(pycurl.USERPWD, '%s:%s' % (username, password) )
319  logging.debug('going to connect to server at: %s' % url )
320 
321  self.curl.perform()
322  code = self.curl.getinfo(pycurl.RESPONSE_CODE)
323  logging.debug('got: %s ', str(code))
324  if code in ( 502,503,504 ):
325  logging.debug('Trying again after %d seconds...', waitForRetry)
326  time.sleep( waitForRetry )
327  response = cStringIO.StringIO()
328  self.curl.setopt(pycurl.WRITEFUNCTION, response.write)
329  self.curl.setopt(pycurl.USERPWD, '%s:%s' % (username, password) )
330  self.curl.perform()
331  code = self.curl.getinfo(pycurl.RESPONSE_CODE)
332  resp = response.getvalue()
333  errorMsg = None
334  if code==500 and not resp.find("INVALID_CREDENTIALS")==-1:
335  logging.error("Invalid credentials provided.")
336  return None
337  if code==403 and not resp.find("Unauthorized access")==-1:
338  logging.error("Unauthorized access. Please check the membership of group 'cms-cond-dropbox'")
339  return None
340  if code==200:
341  try:
342  self.token = json.loads( resp )['token']
343  except Exception as e:
344  errorMsg = 'Error while decoding returned json string'
345  logging.debug('http::getToken> error while decoding json: %s ', str(resp) )
346  logging.debug("error getting token: %s", str(e))
347  resp = None
348  else:
349  errorMsg = 'HTTP Error code %s ' %code
350  logging.debug('got: %s ', str(code))
351  logging.debug('http::getToken> got error from server: %s ', str(resp) )
352  resp = None
353  if resp is None:
354  raise Exception(errorMsg)
355 
356  logging.debug('token: %s', self.token)
357  logging.debug('returning: %s', response.getvalue())
358 
359  return resp
360 
def getToken(self, username, password)
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 361 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, harvestTrackValidationPlots.str, 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().

361  def query(self, url, data = None, files = None, keepCookies = True):
362  '''Queries a URL, optionally with some data (dictionary).
363 
364  If no data is specified, a GET request will be used.
365  If some data is specified, a POST request will be used.
366 
367  If files is specified, it must be a dictionary like data but
368  the values are filenames.
369 
370  By default, cookies are kept in-between requests.
371 
372  A HTTPError exception is raised if the response's HTTP code is not 200.
373  '''
374 
375  if not keepCookies:
376  self.discardCookies()
377 
378  url = self.baseUrl + url
379 
380  # make sure the logs are safe ... at least somewhat :)
381  data4log = copy.copy(data)
382  if data4log:
383  if 'password' in data4log.keys():
384  data4log['password'] = '*'
385 
386  retries = [0] + list(self.retries)
387 
388  while True:
389  logging.debug('Querying %s with data %s and files %s (retries left: %s, current sleep: %s)...', url, data4log, files, len(retries), retries[0])
390 
391  time.sleep(retries.pop(0))
392 
393  try:
394  self.curl.setopt(self.curl.URL, url)
395  self.curl.setopt(self.curl.HTTPGET, 1)
396 
397  # from now on we use the token we got from the login
398  self.curl.setopt(pycurl.USERPWD, '%s:""' % ( str(self.token), ) )
399  self.curl.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
400 
401  if data is not None or files is not None:
402  # If there is data or files to send, use a POST request
403 
404  finalData = {}
405 
406  if data is not None:
407  finalData.update(data)
408 
409  if files is not None:
410  for (key, fileName) in files.items():
411  finalData[key] = (self.curl.FORM_FILE, fileName)
412  self.curl.setopt( self.curl.HTTPPOST, finalData.items() )
413 
414  self.curl.setopt(pycurl.VERBOSE, 0)
415 
416  response = cStringIO.StringIO()
417  self.curl.setopt(self.curl.WRITEFUNCTION, response.write)
418  self.curl.perform()
419 
420  code = self.curl.getinfo(self.curl.RESPONSE_CODE)
421 
422  if code in self.retryCodes and len(retries) > 0:
423  logging.debug('Retrying since we got the %s error code...', code)
424  continue
425 
426  if code != 200:
427  raise HTTPError(code, response.getvalue())
428 
429  return response.getvalue()
430 
431  except pycurl.error as e:
432  if len(retries) == 0:
433  raise e
434  logging.debug('Retrying since we got the %s pycurl exception...', str(e))
435 
436 # common/http.py end
437 
def query(self, url, data=None, files=None, keepCookies=True)
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 264 of file uploadConditions.py.

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

Referenced by uploadConditions.HTTP.__init__().

264  def setBaseUrl(self, baseUrl = ''):
265  '''Allows to set a base URL which will be prefixed to all the URLs
266  that will be queried later.
267  '''
268  self.baseUrl = baseUrl
269 
270 
def setBaseUrl(self, baseUrl='')
def uploadConditions.HTTP.setProxy (   self,
  proxy = '' 
)
Allows to set a proxy.

Definition at line 271 of file uploadConditions.py.

271  def setProxy(self, proxy = ''):
272  '''Allows to set a proxy.
273  '''
274  self.curl.setopt(self.curl.PROXY, proxy)
275 
276 
def setProxy(self, proxy='')
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 283 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.__init__().

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

Definition at line 277 of file uploadConditions.py.

277  def setTimeout(self, timeout = 0):
278  '''Allows to set a timeout.
279  '''
280  self.curl.setopt(self.curl.TIMEOUT, timeout)
281 
282 
def setTimeout(self, timeout=0)

Member Data Documentation

uploadConditions.HTTP.baseUrl
uploadConditions.HTTP.curl

Definition at line 240 of file uploadConditions.py.

uploadConditions.HTTP.retries

Definition at line 296 of file uploadConditions.py.

Referenced by uploadConditions.HTTP.query().

uploadConditions.HTTP.token