pyicap (version 1.0)
index
/home/netom/hobbi/pyicap/pyicap.py

Implements an ICAP server framework
 
For the ICAP specification, see RFC 3507

 
Modules
       
SocketServer
random
socket
sys
time
urlparse

 
Classes
       
SocketServer.StreamRequestHandler(SocketServer.BaseRequestHandler)
BaseICAPRequestHandler
SocketServer.TCPServer(SocketServer.BaseServer)
ICAPServer
exceptions.Exception(exceptions.BaseException)
ICAPError

 
class BaseICAPRequestHandler(SocketServer.StreamRequestHandler)
    ICAP request handler base class.
 
You have to subclass it and provide methods for each service
endpoint. Every endpoint MUST have an _OPTION method, and either
a _REQMOD or a _RESPMOD method.
 
 
Method resolution order:
BaseICAPRequestHandler
SocketServer.StreamRequestHandler
SocketServer.BaseRequestHandler

Methods defined here:
address_string(self)
Return the client address formatted for logging.
 
This version looks up the full hostname using gethostbyaddr(),
and tries to find a name that contains at least one dot.
cont(self)
Send a 100 continue reply
 
Useful when the client sends a preview request, and we have
to read the entire message body. After this command, read_chunk
can safely be called again.
date_time_string(self, timestamp=None)
Return the current date and time formatted for a message header.
handle(self)
Handles a connection
 
Since we support Connection: keep-alive, moreover this is the
default behavior, one connection may mean multiple ICAP
requests.
handle_one_request(self)
Handle a single HTTP request.
 
You normally don't need to override this method; see the class
__doc__ string for information on how to handle specific HTTP
commands such as GET and POST.
log_date_time_string(self)
Return the current time formatted for logging.
log_error(self, format, *args)
Log an error.
 
This is called when a request cannot be fulfilled.  By
default it passes the message on to log_message().
 
Arguments are the same as for log_message().
 
XXX This should go to the separate error log.
log_message(self, format, *args)
Log an arbitrary message.
 
This is used by all other logging functions.  Override
it if you have specific logging wishes.
 
The first argument, FORMAT, is a format string for the
message to be logged.  If the format string contains
any % escapes requiring parameters, they should be
specified as subsequent arguments (it's just like
printf!).
 
The client ip address and current date/time are prefixed to every
message.
log_request(self, code='-', size='-')
Log an accepted request.
 
This is called by send_response().
no_adaptation_required(self)
Tells the client to leave the message unaltered
 
If the client allows 204, or this is a preview request than
a 204 preview response is sent. Otherwise a copy of the message
is returned to the client.
parse_request(self)
Parse a request (internal).
 
The request should be stored in self.raw_requestline; the results
are in self.command, self.request_uri, self.request_version and
self.headers.
 
Return True for success, False for failure; on failure, an
error is sent back.
read_chunk(self)
Read a HTTP chunk
 
Also handles the ieof chunk extension defined by the ICAP
protocol by setting the ieof variable to True. It returns an
empty line if the last chunk is read. Further reads might hang
forever.
send_error(self, code, message=None)
Send and log an error reply.
 
Arguments are the error code, and a detailed message.
The detailed message defaults to the short entry matching the
response code.
 
This sends an error response (so it must be called before any
output has been generated), logs the error, and finally sends
a piece of HTML explaining the error to the user.
send_headers(self, has_body=False)
Send ICAP and encapsulated headers
 
Assembles the Encapsulated header, so it's need the information
of wether an encapsulated message body is present.
set_enc_header(self, header, value)
Set an encapsulated header to the given value
 
Multiple sets will cause the header to be sent multiple times.
set_enc_request(self, request)
Set encapsulated request line in response
 
ICAP responses can only contain one encapsulated header section.
Such section is either an encapsulated HTTP request, or a
response. This method can be called to set encapsulated HTTP
request's request line.
set_enc_status(self, status)
Set encapsulated status in response
 
ICAP responses can only contain one encapsulated header section.
Such section is either an encapsulated HTTP request, or a
response. This method can be called to set encapsulated HTTP
response's status line.
set_icap_header(self, header, value)
Set an ICAP header to the given value
 
Multiple sets will cause the header to be sent multiple times.
set_icap_response(self, code)
Sets the ICAP response's status line and response code
setup(self)
Initialize the handler
Assignes default values to every field of this object
version_string(self)
Return the server software version string.
write_chunk(self, data)
Write a chunk of data
 
When finished writing, an empty chunk with data='' must
be written.

Data and other attributes defined here:
protocol_version = 'ICAP/1.0'

Methods inherited from SocketServer.StreamRequestHandler:
finish(self)

Data and other attributes inherited from SocketServer.StreamRequestHandler:
rbufsize = -1
wbufsize = 0

Methods inherited from SocketServer.BaseRequestHandler:
__init__(self, request, client_address, server)

 
class ICAPError(exceptions.Exception)
    Signals a protocol error
 
 
Method resolution order:
ICAPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods defined here:
__init__(self, code=500, message=None)

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
class ICAPServer(SocketServer.TCPServer)
    ICAP Server
 
This is a simple TCPServer, that allows address reuse
 
 
Method resolution order:
ICAPServer
SocketServer.TCPServer
SocketServer.BaseServer

Data and other attributes defined here:
allow_reuse_address = 1

Methods inherited from SocketServer.TCPServer:
__init__(self, server_address, RequestHandlerClass, bind_and_activate=True)
Constructor.  May be extended, do not override.
close_request(self, request)
Called to clean up an individual request.
fileno(self)
Return socket file number.
 
Interface required by select().
get_request(self)
Get the request and client address from the socket.
 
May be overridden.
server_activate(self)
Called by constructor to activate the server.
 
May be overridden.
server_bind(self)
Called by constructor to bind the socket.
 
May be overridden.
server_close(self)
Called to clean-up the server.
 
May be overridden.

Data and other attributes inherited from SocketServer.TCPServer:
address_family = 2
request_queue_size = 5
socket_type = 1

Methods inherited from SocketServer.BaseServer:
finish_request(self, request, client_address)
Finish one request by instantiating RequestHandlerClass.
handle_error(self, request, client_address)
Handle an error gracefully.  May be overridden.
 
The default is to print a traceback and continue.
handle_request(self)
Handle one request, possibly blocking.
 
Respects self.timeout.
handle_timeout(self)
Called if no new request arrives within self.timeout.
 
Overridden by ForkingMixIn.
process_request(self, request, client_address)
Call finish_request.
 
Overridden by ForkingMixIn and ThreadingMixIn.
serve_forever(self, poll_interval=0.5)
Handle one request at a time until shutdown.
 
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.
shutdown(self)
Stops the serve_forever loop.
 
Blocks until the loop has finished. This must be called while
serve_forever() is running in another thread, or it will
deadlock.
verify_request(self, request, client_address)
Verify the request.  May be overridden.
 
Return True if we should proceed with this request.

Data and other attributes inherited from SocketServer.BaseServer:
timeout = None

 
Data
        __all__ = ['ICAPServer', 'BaseICAPRequestHandler', 'ICAPError']
__version__ = '1.0'