Python Manual Benutzerhandbuch

Seite von 124
The Redirect Servlet
93
The Redirect Servlet
The redirect servlet as implemented by the
RedirectServlet
class would be used when it is nec-
essary to redirect a HTTP client to an alternate resource. In addition to the HTTP session object, it
should be supplied the URI of the resource to which the HTTP client is to be directed. By default, the
HTTP response code will be "
302
", indicating the resource has been temporarily moved. This can be
explicitly indicated by using the value "
REDIRECT_TEMPORARY
". If the resource has been perma-
nently moved, the value "
REDIRECT_PERMANENT
" can instead be used.
class HttpServer(netsvc.HttpServer):
def servlet(self,session):
url = "http://hostname/" + session.servletPath()
type = netsvc.REDIRECT_PERMANENT
return netsvc.RedirectServlet(session,url,type)
If the URI doesn’t start with "
/
", it is assumed to be a valid URI and will be passed as is. If the URI
starts with "
/
", it will assumed to be a absolute URL against the current server host and will be auto-
matically adjusted to include the details of the server host in the URL.
The Echo Servlet
The echo servlet as implemented by the
EchoServlet
class is useful for debugging. When used to
service a HTTP request, it will generate a HTML document which provides details about the request.
class HttpServer(netsvc.HttpServer):
def servlet(self,session):
return netsvc.EchoServlet(session)
The File Servlet
The file servlet as implemented by the
FileServlet
class, is used to deliver up to a HTTP client
the contents of a file stored in the operating system’s filesystem. This is the same servlet which is used
internal to the
FileServer
class. When this servlet is being created it needs to be supplied with the
name of the file and the file type. The latter corresponds to the MIME content type included in the
HTTP response.
If the path supplied to the
FileServlet
class actually describes a directory, the servlet will generate
a response indicating that access is forbidden. If you wish to implement directory browsing you will
need to implement a separate HTTP servlet to generate an appropriate response and map the request
to it. If you want to redirect the request to an index file, your HTTP server should determine if such an
index file exists and if it does, create the file servlet against it instead.
When the
FileServlet
class is used, any size file can be handled without the size of the application
growing in size and without the application blocking as a result of a slow HTTP client. This is achieved
as a result of the file being sent in blocks, with the servlet waiting if the connection to the HTTP client