source: TI05-delivery/trunk/lib/python/delivery/server.py @ 1133

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/lib/python/delivery/server.py@1133
Revision 1133, 3.4 KB checked in by spascoe, 14 years ago (diff)

The python delivery package is born. See the delivery.server module for
the server API. This module imports delivery.bbftpd which does the python/C
binding.

The delivery package is being documented using epydoc comments
(epydoc.sourceforge.net). Although the latest version of doxygen will do
python it isn't installed on glue and epydoc is more mature.

RevLine 
[1133]1"""
2Interface to the BBftp server.
3
4@author Stephen Pascoe
5"""
6
7import bbftpd
8
9def start(authHandler):
10    """Start the bbftp server.
11
12    The server performs a fork() during initialisation, the child process remains in
13    the server's main loop and the parent returns from this function.  The server process
14    calls authHandler() on each connection to do authentication/authorisation.
15
16    @note: because the server process forks, authHandler will not see any changes to the python
17        interpreter following the call to start().
18
19    @return: the PID of the server process.
20    """
21   
22    bbftpd.run(authHandler)
23
24
25class AuthHandler(object):
26    """Abstract base class for implementing authentication.
27
28    Subclasses should implement the authorise() method to authenticate clients connecting to
29    the server.  An AuthHandler object is passed to start() when initialising the server.
30    """
31   
32    def __call__(self):
33        """Entry point called by the server to do authorisation.
34
35        This function simply calls the authorise() method.  See authorise() for details.
36        """
37        return self.authorise()
38
39    def send(self, buffer):
40        """Send a message to the client.
41
42        @param buffer a string containing the message to send.
43        """
44   
45        return bbftpd.send(buffer)
46
47    def recv(self):
48        """Receive a message from the client.
49
50        @return a string containing the message received.
51        """
52
53        return bbftpd.recv()
54
55    def authorise(self):
56        """Authenticate a connection.
57
58        This function should be overridden in subclasses to implement authentication using
59        the method calls self.send() and send.recv().
60
61        @note because the server process forks on each connection, calls to authorise() will
62            see the state of the python interpreter as it was when start() was called.
63
64        @return an AuthzHandler instance if authentication succeeds, else None.
65        """
66
67        raise NotImplementedError
68
69
70class AuthzHandler(object):
71    """Abstract base class for implementing authorisation.
72    """
73
74    def authzControl(self, msgcode, transferoption, path):
75        """Authorise a control command.
76
77        @param msgcode: the command type.
78        @param transferoption: extra options specified in the command.
79        @param path: the file or directory to which the command applies.
80        @return: 0 on success or -1 on failure.
81        """
82
83        raise NotImplementedError
84
85    def authzRetr(self, path):
86        """Authorise a retrieve request.
87
88        @param path: the file being retrieved.
89        @return: 0 on success or -1 on failure.
90        """
91
92        raise NotImplementedError
93
94    def authzSend(self, path):
95        """Authorise a send request.
96
97        @param path: the destination file.
98        @return: 0 on success or -1 on failure.
99        """
100
101
102
103
104class BasicClientAuthHandler(AuthHandler):
105    """
106    When testing with the basic bbftp client some messages will be NULL terminated.  This class
107    makes communicating with such clients slightly easier.
108    """
109
110    def recvCStr(self):
111        """Receive a message from the client which may contain \\0 characters indicating message end.
112
113        The message is truncated to the first \\0 character.
114
115        @return a string containing the message received.
116        """
117
118        msg = self.recv()
119        # Trim to first '\0'
120        x = msg.find('\0')
121        if x:
122            msg = msg[:x]
123
124        return msg
125
Note: See TracBrowser for help on using the repository browser.