Python 7.0pl5 Electronic Accessory User Manual


 
Servlet Objects
100
def processContent(self,content):
self._content.append(content)
self._contentLength = self._contentLength + len(content)
if self._contentLength >= self.contentLength():
self._environ["REQUEST_METHOD"] = self.requestMethod()
self._content = string.join(self._content,"")
self._content = self._content[:self.contentLength()]
fp = StringIO.StringIO(self._content)
try:
form = cgi.FieldStorage(headers=self._headers, \
environ=self._environ,keep_blank_values=1,fp=fp)
self.processForm(form)
except:
netsvc.logException()
self.shutdown()
def processForm(self,form):
self.sendResponse(501)
Member functions which a HTTP servlet may find useful here are "contentLength()" and "con-
tentType()". The "contentLength()" member function returns an integer value correspond-
ing to that defined by the HTTP content length header, or "-1" if no such field was provided. The
"contentType()" member function returns the HTTP content type header. Note that this will in-
clude any supplied parameters so you will need to extract these yourself.
A HTTP servlet may also interrogate arbitrary headers using the member functions "contains-
Header()" and "header()". These respectively indicate if a header exists and return its value. The
name of a header should always be given as a lower case string. All headers may be obtained as a Py-
thon dictionary using "headers()".
If a HTTP servlet encounters an internal error at any time, it may call the "shutdown()" member
function to abort all processing of the request. This will cause the connection to the HTTP client to be
closed immediately, discarding any data which hadn’t yet been sent. The instance of the HTTP servlet
will then subsequently be destroyed.
The Form Servlet
As processing of form data will be a common situation, an implementation of a form servlet is provid-
ed.ThisiscalledFormServlet. The implementation of this servlet is similar to the previous exam-
ple except that it does additional processing to translate data from the types used by the
FieldStorage class into standard Python lists and dictionaries. The name of the member function
which you need to override to process the form is "handleRequest()".
class LoginServlet(netsvc.FormServlet):
def handleRequest(self):
if self.containsField("user") and \
self.containsField("password"):
user = self.field("user")