Python Manual Benutzerhandbuch

Seite von 124
Servlet Framework
90
When an instance of the
FileServer
class is created, it must be supplied with the filesystem direc-
tory from which files are to be served. The server object utilises the Python
mimetypes
module for
determining file types. The file type associated with an extension can be overridden, or knowledge of
additional file types can be added using the "
map()
" member function.
filesrvr = netsvc.FileServer("/home/httpd")
filesrvr.map(".py","text/plain")
Note that it is expected that the HTTP client knows the name of the file it is trying to access as there
is no builtin support included for directory browsing. It is however possible to define the names of one
or more index files to try when a request identifies a directory as opposed to a file. When more than
one index file is specified, those which were declared later, take precedence.
filesrvr.index("index.htm")
filesrvr.index("index.html")
Editor backup files, temporary files generated by an application, or any other files which should not in
any way be accessible from a HTTP client, can be hidden from view so long as they have a distinct
extension.
filesrvr.hide(".bak")
filesrvr.hide(".html~")
When it comes to the actual task of serving up a single file from the file system, the
FileServlet
class is used. This is a wrapper around the corresponding servlet class from the OSE C++ class library
used to handle the request for a single file. The servlet may be used directly from a custom HTTP serv-
er object.
Client Authorisation
If the HTTP servlet framework is being used to provide an administrative interface into an application,
it may be desirable to block access from all but a few selected client hosts. This can be useful where
the application is otherwise intentionally accessible over the Internet, or may inadvertantly become ac-
cessible from a broader range of hosts than intended. This may result from misconfigured firewalls, or
the addition of additional subnets to a corporate network.
If you wish to control who can access the application through the port monitored by the HTTP daemon,
it is necessary to create a derived version of the
HttpDaemon
class and override the "
author-
ise()
" member function. For each client connection, this member function will be called with the IP
address of the client host. Your code can thereby block requests from any undesirable hosts.
class HttpDaemon(netsvc.HttpDaemon):
def __init__(self,port,hosts=[]):
netsvc.HttpDaemon.__init__(self,port)
self._allow = hosts
def authorise(self,host):
return host in self._allow