Python Python Manual Electronic Accessory User Manual


 
Processing Content
99
been derived from the Agent class, this would include calling the "destroyReferences()"
member function.
Processing Content
If the function of a HTTP servlet entails that the content associated with a request be processed in some
way, it will be necessary to override the "processContent()" member function. The "process-
Content()" member function will only be called subsequent to "processRequest()"being
called, and only provided that "processRequest()" hadn’t already dealt with the request and sent
a complete response.
As the means to determine how much content to expect is dependent on the specifics of a request, no
attempt is made to first accumulate the content into one block. Instead, the "processContent()"
member function will be called multiple times if appropriate, once for each block of data which is read
in. It is up to the "processContent()" member function to accumulate the data or otherwise proc-
ess it, until it determines that all content has been received.
Typically, how much content is expected will be dictated by the presence of a HTTP content length
header, or by a MIME multipart message boundary string as specificed in a HTTP content type header.
Either way, it is up to the specific implementation of a HTTP servlet to know what to expect and deal
with it appropriately.
class FormServlet(netsvc.HttpServlet):
def processRequest(self):
if self.requestMethod() not in ["GET","POST"]:
self.sendError(501,"Request method type is not supported.")
elif self.requestMethod() == "POST" \
and self.contentLength() < 0:
self.sendError(400,"Content length required for POST.")
elif self.requestMethod() == "GET":
self._environ = {}
self._content = []
self._contentLength = 0
self._headers = self.headers()
self._environ["REQUEST_METHOD"] = self.requestMethod()
self._environ["QUERY_STRING"] = self.queryString()
self._headers["content-type"] = \
"application/x-www-form-urlencoded"
try:
form = cgi.FieldStorage(headers=self._headers, \
environ=self._environ,keep_blank_values=1)
self.processForm(form)
except:
netsvc.logException()
self.shutdown()
elif self.contentLength() == 0:
self.processContent("")