Python Python Manual, 7.0pl5 manual Http Daemon

Models: Python Manual 7.0pl5

1 124
Download 124 pages 59.17 Kb
Page 88
Image 88

Servlet Framework

to provide a servlet to handle the actual request. If no server object is found corresponding to that por- tion of the URL namespace, or the server object is not able to provide a servlet to handle the request, a HTTP error response is returned to the client indicating that the resource corresponding to the sup- plied URL could not be found.

Where an appropriate servlet to handle the request is found, the session manager will initially pass off to the servlet the details of the request. This will include the type of request, the URL and the contents of any HTTP headers. The details initially provided to the servlet do not include any content associated with the request. Any content associated with a request will subsequently be passed to the servlet as it arrives. This will only occur though if the servlet wasn’t able to process the request based on the initial information and does actually require the content.

In the majority of cases a request will not have any associated content and a servlet will be able to proc- ess the request straight away. Even if there is no content however, the servlet isn’t obligated to send a response immediately. This may be the situation if the servlet needs to wait until information from an- other source arrives before it can form the response. In this scenario, the servlet might send a request using the messaging framework to a remote service to obtain the information. When the response from the remote service arrives, the servlet can then generate the response.

When the action of the servlet does depend on the content supplied with the request, the servlet would accumulate the content as it arrives until the amount of content matches that given in the content length header, or until some appropriate boundary is encountered. Now having all the content associated with the request, the servlet can process the request and send a response. Alternatively it could again delay the response if it needs to first send the content received to some remote service and wait for some re- sponse.

When a servlet sends a response to the HTTP client, as long as the servlet generates a content length in the HTTP headers, any request by a HTTP client to keep alive the session will be honoured. This allows the HTTP client to submit additional requests using the same connection if desired. In general the servlet framework adheres to the HTTP 1.0 protocol.

The HTTP Daemon

The Python class which listens for connection requests from HTTP clients is called HttpDaemon. When creating the HTTP daemon, you need to tell it which port to listen on and also register with it any HTTP server objects. When registering a HTTP server object, you need to identify which part of the URL namespace it manages. Finally, you need to start the daemon so that once the dispatcher is run it will actually listen and handle the requests.

dispatcher = netsvc.Dispatcher() dispatcher.monitor(signal.SIGINT)

daemon = netsvc.HttpDaemon(8000)

filesrvr = netsvc.FileServer(os.getcwd()) daemon.attach("/",filesrvr)

88

Page 88
Image 88
Python Python Manual, 7.0pl5 manual Http Daemon