📄 __init__.py
字号:
## Copyright (c) 2005, John Mettraux, OpenWFE.org# All rights reserved.# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met:# # . Redistributions of source code must retain the above copyright notice, this# list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution.# # . Neither the name of the "OpenWFE" nor the names of its contributors may be# used to endorse or promote products derived from this software without# specific prior written permission.# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE.## $Id: __init__.py 2098 2005-09-06 17:05:37Z jmettraux $#""" A base class for interacting with an OpenWFE REST service"""import base64from time import sleepfrom urllib2 import urlopenfrom urllib2 import Requestfrom openwfe.workitem import codec## the number of times unsuccesful requests are retried##RETRY = 14RETRY = 3HTTP_EOL = '\r\n'DOUBLE_HTTP_EOL = HTTP_EOL + HTTP_EOLclass RestSession: """ The basics of a REST session. Do not instantiate directly. """ def _computeUrl (self, action, storeName, **params): if storeName: url = '%s/%s' % (self.url, storeName) else: url = self.url url = '%s?session=%s&action=%s' % (url, self.sessionId, action) for k in params.keys(): url += '&%s=%s' % (k, params[k]) return str(url) def _get (self, action, storeName=None, **params): return urlopen(self._computeUrl(action, storeName, **params)) def _getXml (self, action, storeName=None, **params): for l in self._get(action, storeName, **params).readlines(): print l[:-1] def _post (self, action, storeName, data, **params): url = self._computeUrl(action, storeName, **params) # #print "urlopen('%s', '%s')" % (url, data) # if data: data = data + DOUBLE_HTTP_EOL return urlopen(url, data) def _doPost (self, action, storeName, data, **params): #printFirstLine(data) r = None retry = 0 while 1: try: r = self._post(action, storeName, data, **params) break except: if retry == RETRY-1: sleep(1) if retry > RETRY: raise #print '============================ retry = %i' % retry retry = retry + 1 return codec.decode(r) # debug #import codecs #r = codecs.EncodedFile(r, 'iso-8859-1') #rd = r.read() #f = codecs.open('/home/jmettraux/tmp/python.out', 'w', 'iso-8859-1') #f.write(rd) #f.flush() #f.close() #return codec.decode(rd) def _postXml (self, action, storeName, data, **params): for l in self._post(action, storeName, data, **params).readlines(): print l[:-1] def close (self): """ Terminates the worksession. You should take care of using this method, it frees ressources in the worklist. """ self._doPost('endWorkSession', None, None)## some methodsdef getSession (url, username, password): """ Connects to the given url, login and return the new sessionId if any. """ hs = {} hs['Authorization'] = 'Basic %s' % \ base64.encodestring('%s:%s' % (username, password)) hs['RestClient'] = \ '$Id: __init__.py 2098 2005-09-06 17:05:37Z jmettraux $' request = Request(url, headers=hs) f = urlopen(request) return codec.decode(f)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -