Python Manual Benutzerhandbuch

Seite von 124
The File Server
89
daemon.start()
dispatcher.run()
When a HTTP server object is registered, the first argument to the "
attach()
" member function
should be the path under which resources made available by the HTTP server object are accessible.
Except for the root directory, the path should not include a trailing "
/
". The path should also be in nor-
malised form. That is, it should not include consecutive instances of "
/
" within the path, or include the
path components "
..
" or "
.
".
If the path isn’t normalised in this respect, these paths will never match against any request as request
URLs will always be normalised before attempting a match. The request URL is always normalised to
avoid the possibility of malicious requests trying to access file type resources outside the available di-
rectory tree.
If desired, a single HTTP server object may be registered multiple times within the one URL name-
space. Registrations may also be done hierachically. That is, one registration may nest within the URL
namespace of another. In this situation a request will match against the HTTP server object with the
most deeply nested path.
filesrvr1 = netsvc.FileServer(os.path.join(os.getcwd(),"info"))
filesrvr2 = netsvc.FileServer(os.path.join(os.getcwd(),"logs"))
daemon.attach("/",filesrvr1)
daemon.attach("/logs",filesrvr2)
Normally the port which the HTTP daemon is to listen on will be fixed. If you require a dynamically
allocated port, you should use "
0
" as the port number. The actual port number which is allocated can
then be queried using the "
port()
" member function. Obviously, this port number would then need
to be displayed somewhere or otherwise accessible so it is known which port to connect to.
daemon = netsvc.HttpDaemon(0)
port = daemon.port()
The File Server
The
FileServer
class is a predefined HTTP server object for serving up files from the file system
in response to HTTP GET requests. This server object is suitable for providing access to documenta-
tion related to an application, configuration files or application log files. A plugin mechanism for han-
dling special file types is also included
In the case of files resident in the file system, the server is able to handle any size file, with the corre-
sponding servlet only sending data back to the HTTP client as it is able to receive it. That is, transmis-
sion of a large file will not blow out the size of the application nor will it cause the application to block
if the client is slow at reading the contents of the file.