CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes
upload_popcon.HTTP Class Reference
Inheritance diagram for upload_popcon.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 64 of file upload_popcon.py.

Constructor & Destructor Documentation

◆ __init__()

def upload_popcon.HTTP.__init__ (   self)

Definition at line 70 of file upload_popcon.py.

70  def __init__(self):
71  self.setBaseUrl()
72  self.setRetries()
73 
74  self.curl = pycurl.Curl()
75  self.curl.setopt(self.curl.COOKIEFILE, '') # in memory
76 
77  #-toDo: make sure we have the right options set here to use ssl
78  #-review(2015-09-25): check and see - action: AP
79  # self.curl.setopt(self.curl.SSL_VERIFYPEER, 1)
80  self.curl.setopt(self.curl.SSL_VERIFYPEER, 0)
81  self.curl.setopt(self.curl.SSL_VERIFYHOST, 2)
82 
83  self.baseUrl = None
84 
85  self.token = None
86 

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

Member Function Documentation

◆ discardCookies()

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

Definition at line 92 of file upload_popcon.py.

92  def discardCookies(self):
93  '''Discards cookies.
94  '''
95  self.curl.setopt(self.curl.COOKIELIST, 'ALL')
96 
97 

References upload_popcon.HTTP.curl.

Referenced by upload_popcon.HTTP.query(), and uploadConditions.HTTP.query().

◆ getCookies()

def upload_popcon.HTTP.getCookies (   self)
Returns the list of cookies.

Definition at line 87 of file upload_popcon.py.

87  def getCookies(self):
88  '''Returns the list of cookies.
89  '''
90  return self.curl.getinfo(self.curl.INFO_COOKIELIST)
91 

References upload_popcon.HTTP.curl.

◆ getToken()

def upload_popcon.HTTP.getToken (   self,
  username,
  password 
)

Definition at line 132 of file upload_popcon.py.

132  def getToken(self, username, password):
133 
134  url = self.baseUrl + 'token'
135 
136  self.curl.setopt(pycurl.URL, url)
137  self.curl.setopt(pycurl.VERBOSE, 0)
138 
139  #-toDo: check if/why these are needed ...
140  #-ap: hmm ...
141  # self.curl.setopt(pycurl.DNS_CACHE_TIMEOUT, 0)
142  # self.curl.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4)
143  #-end hmmm ...
144  #-review(2015-09-25): check and see - action: AP
145 
146 
147  self.curl.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
148  # self.curl.setopt( self.curl.POST, {})
149  self.curl.setopt(self.curl.HTTPGET, 0)
150 
151  response = cStringIO.StringIO()
152  self.curl.setopt(pycurl.WRITEFUNCTION, response.write)
153  self.curl.setopt(pycurl.USERPWD, '%s:%s' % (username, password) )
154 
155  logging.debug('going to connect to server at: %s' % url )
156 
157  self.curl.perform()
158  code = self.curl.getinfo(pycurl.RESPONSE_CODE)
159  logging.debug('got: %s ', str(code))
160 
161  try:
162  self.token = json.loads( response.getvalue() )['token']
163  except Exception as e:
164  logging.error('http::getToken> got error from server: %s ', str(e) )
165  if 'No JSON object could be decoded' in str(e):
166  return None
167  logging.error("error getting token: %s", str(e))
168  return None
169 
170  logging.debug('token: %s', self.token)
171  logging.debug('returning: %s', response.getvalue())
172 
173  return response.getvalue()
174 

References upload_popcon.HTTP.baseUrl, upload_popcon.HTTP.curl, str, and upload_popcon.HTTP.token.

◆ query()

def upload_popcon.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 175 of file upload_popcon.py.

175  def query(self, url, data = None, files = None, keepCookies = True):
176  '''Queries a URL, optionally with some data (dictionary).
177 
178  If no data is specified, a GET request will be used.
179  If some data is specified, a POST request will be used.
180 
181  If files is specified, it must be a dictionary like data but
182  the values are filenames.
183 
184  By default, cookies are kept in-between requests.
185 
186  A HTTPError exception is raised if the response's HTTP code is not 200.
187  '''
188 
189  if not keepCookies:
190  self.discardCookies()
191 
192  url = self.baseUrl + url
193 
194  # make sure the logs are safe ... at least somewhat :)
195  data4log = copy.copy(data)
196  if data4log:
197  if 'password' in data4log.keys():
198  data4log['password'] = '*'
199 
200  retries = [0] + list(self.retries)
201 
202  while True:
203  logging.debug('Querying %s with data %s and files %s (retries left: %s, current sleep: %s)...', url, data4log, files, len(retries), retries[0])
204 
205  time.sleep(retries.pop(0))
206 
207  try:
208  self.curl.setopt(self.curl.URL, url)
209  self.curl.setopt(self.curl.HTTPGET, 1)
210 
211  # from now on we use the token we got from the login
212  self.curl.setopt(pycurl.USERPWD, '%s:""' % ( str(self.token), ) )
213  self.curl.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
214 
215  if data is not None or files is not None:
216  # If there is data or files to send, use a POST request
217 
218  finalData = {}
219 
220  if data is not None:
221  finalData.update(data)
222 
223  if files is not None:
224  for (key, fileName) in files.items():
225  finalData[key] = (self.curl.FORM_FILE, fileName)
226  self.curl.setopt( self.curl.HTTPPOST, finalData.items() )
227 
228  self.curl.setopt(pycurl.VERBOSE, 0)
229 
230  response = cStringIO.StringIO()
231  self.curl.setopt(self.curl.WRITEFUNCTION, response.write)
232  self.curl.perform()
233 
234  code = self.curl.getinfo(self.curl.RESPONSE_CODE)
235 
236  if code in self.retryCodes and len(retries) > 0:
237  logging.debug('Retrying since we got the %s error code...', code)
238  continue
239 
240  if code != 200:
241  raise HTTPError(code, response.getvalue())
242 
243  return response.getvalue()
244 
245  except pycurl.error as e:
246  if len(retries) == 0:
247  raise e
248  logging.debug('Retrying since we got the %s pycurl exception...', str(e))
249 
250 # common/http.py end
251 

References upload_popcon.HTTP.baseUrl, upload_popcon.HTTP.curl, upload_popcon.HTTP.discardCookies(), upload_popcon.HTTP.retries, str, and upload_popcon.HTTP.token.

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

◆ setBaseUrl()

def upload_popcon.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 98 of file upload_popcon.py.

98  def setBaseUrl(self, baseUrl = ''):
99  '''Allows to set a base URL which will be prefixed to all the URLs
100  that will be queried later.
101  '''
102  self.baseUrl = baseUrl
103 
104 

References upload_popcon.HTTP.baseUrl.

Referenced by upload_popcon.HTTP.__init__(), and uploadConditions.HTTP.__init__().

◆ setProxy()

def upload_popcon.HTTP.setProxy (   self,
  proxy = '' 
)
Allows to set a proxy.

Definition at line 105 of file upload_popcon.py.

105  def setProxy(self, proxy = ''):
106  '''Allows to set a proxy.
107  '''
108  self.curl.setopt(self.curl.PROXY, proxy)
109 
110 

References upload_popcon.HTTP.curl.

◆ setRetries()

def upload_popcon.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 117 of file upload_popcon.py.

117  def setRetries(self, retries = ()):
118  '''Allows to set retries.
119 
120  The retries are a sequence of the seconds to wait per retry.
121 
122  The retries are done on:
123  * PyCurl errors (includes network problems, e.g. not being able
124  to connect to the host).
125  * 502 Bad Gateway (for the moment, to avoid temporary
126  Apache-CherryPy issues).
127  * 503 Service Temporarily Unavailable (for when we update
128  the frontends).
129  '''
130  self.retries = retries
131 

Referenced by upload_popcon.HTTP.__init__(), and uploadConditions.HTTP.__init__().

◆ setTimeout()

def upload_popcon.HTTP.setTimeout (   self,
  timeout = 0 
)
Allows to set a timeout.

Definition at line 111 of file upload_popcon.py.

111  def setTimeout(self, timeout = 0):
112  '''Allows to set a timeout.
113  '''
114  self.curl.setopt(self.curl.TIMEOUT, timeout)
115 
116 

References upload_popcon.HTTP.curl.

Member Data Documentation

◆ baseUrl

upload_popcon.HTTP.baseUrl

◆ curl

upload_popcon.HTTP.curl

◆ retries

upload_popcon.HTTP.retries

Definition at line 130 of file upload_popcon.py.

Referenced by upload_popcon.HTTP.query(), and uploadConditions.HTTP.query().

◆ token

upload_popcon.HTTP.token
str
#define str(s)
Definition: TestProcessor.cc:53
contentValuesFiles.query
query
Definition: contentValuesFiles.py:38
EcalCondTools.getToken
def getToken(db, tag, since)
Definition: EcalCondTools.py:334