Python Manual Benutzerhandbuch

Seite von 124
User Authorisation
91
If access to a particular client is disallowed, the connection will be dropped immediately. The client
will not receive any form of specific HTTP error response indicating why the connection has been
closed. Note that this mechanism blocks a client from accessing any part of the URL namespace for
that HTTP daemon. If you wish to only block client access to specific resources, you would need to
customise each HTTP server object or servlet, or use multiple HTTP daemon objects on separate ports.
User Authorisation
A further level of authorisation beyond that of blocking specific client hosts is to individually authen-
ticate each user. The mechanism for user authentication is performed against the HTTP server objects.
That is, the URL namespace managed by each HTTP server component can be individually protected
using different user databases.
To add user authentication to a particular HTTP server object, you should derive from the class and
override the "
authorise()
" member function. If building your own HTTP server object, you could
embed the member function directly in your class.
class FileServer(netsvc.FileServer):
def __init__(self,directory,users={}):
netsvc.FileServer.__init__(self,directory)
self._allow = users
def authorise(self,login,password):
return self._allow.has_key(login) and \
self._allow[login] == password
If you need to control user access at the level of individual URLs within the URL namespace managed
by a particular HTTP server object, that functionality would need to be embedded into any servlets cre-
ated by that HTTP server object, or managed at the point that the servlets are created by the HTTP serv-
er object. Note that only the HTTP basic authentication mechanism is supported. There is no support
for use of secure sockets and SSL.
HTTP Server Objects
When a HTTP request is received, it is a HTTP server object which will dictate the type of HTTP serv-
let created to handle the request. If you wish to implement a customised mapping between request
URLs and the available HTTP servlets, or introduce a new type of HTTP servlet, you will need to de-
fine your own HTTP server object by deriving from the
HttpServer
class and overriding the
"
servlet()
" member function.
class HttpServer(netsvc.HttpServer):
def servlet(self,session):
servletPath = session.servletPath()
if servletPath == "echo":
return netsvc.EchoServlet(session)
elif servletPath == "motd":
return netsvc.FileServlet(session,"/etc/motd","text/plain")