source: TI05-delivery/trunk/examples/deliveryd.py @ 1289

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/examples/deliveryd.py@1289
Revision 1289, 2.6 KB checked in by spascoe, 13 years ago (diff)

Updated python code to reflect the renaming of the delivery package to pybbftp.
Some C tidying to reduce compiler warnings.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2"""A very simple example of a server.
3
4@author Stephen pascoe
5"""
6
7import sys, os, syslog, stat, time, signal
8from glob import glob
9import re, tempfile, getopt
10
11HOME = os.path.abspath(os.getenv('NDG_DELIVERY_HOME', os.curdir))
12BUILDDIR = glob('%s/build/lib.*' % HOME)[0]
13DATADIR = '%s/test/data' % HOME
14VERSION = open('%s/VERSION' % HOME).read()
15NDG_MESSAGE_LEN = 256
16
17NDG_HANDSHAKE = "NDG-Delivery-server %s" % VERSION
18
19sys.path.append(BUILDDIR)
20
21import pybbftp.server as server
22
23
24class AuthHandler(server.AuthHandler):
25   
26    def authenticate(self):
27        msg = self.recv()
28        syslog.syslog(syslog.LOG_DEBUG, 'AuthContext received Auth message: %s' % msg)
29               
30        self.send(NDG_HANDSHAKE)
31
32        privatestr = self.recv()
33        syslog.syslog(syslog.LOG_DEBUG, "AuthContext received privatestr: %s" % privatestr)
34       
35        return self.makeAuthzHandler(msg, "TestCaseUser")
36
37    def makeAuthzHandler(self, msg, user):
38        return AuthzHandler(msg, "TestCaseUser")
39
40class AuthzHandler(server.LiberalAuthzHandler):
41    def __init__(self, version, username):
42        super(AuthzHandler, self).__init__(username)
43        self.version = version
44
45    def authzControl(self, msgcode, transferopt, path):
46        syslog.syslog(syslog.LOG_DEBUG, 'AuthzHandler.authzControl: msgcode = %s' % hex(msgcode))
47        return self.authzPath(path)
48
49    def authzRetr(self, path):
50        return self.authzPath(path)
51
52    def authzStore(self, path):
53        return self.authzPath(path)
54
55    def authzPath(self, path):
56        # For dir commands the path will end with '*'
57        if path[-2:] == '/*':
58            path = path[:-2]
59        mode = stat.S_IMODE(os.stat(path)[stat.ST_MODE])
60        if (mode & 0004):
61            syslog.syslog(syslog.LOG_DEBUG, 'AuthzHandler OK')
62            return True
63        else:
64            syslog.syslog(syslog.LOG_DEBUG, 'AuthzHandler FAIL')
65            raise server.AuthorisationFailure, "AuthzHandler: no read perms"
66
67
68syslog.openlog('test_embedded.py', 0, syslog.LOG_LOCAL0)
69
70print '''
71===================================
72NDG Delivery Service example server
73===================================
74'''
75
76n = len(open('/var/log/bbftpd/bbftpd.log').readlines())
77pid = server.start(AuthHandler(), ['-l', 'DEBUG'])
78print 'Server process started at pid = %d' % pid
79print 'Monitoring /var/log/bbftpd/bbftpd.log'
80
81try:
82    while 1:
83        newlines = open('/var/log/bbftpd/bbftpd.log').readlines()[n:]
84        n += len(newlines)
85        for line in newlines:
86            print line,
87        time.sleep(2)
88except:
89    os.kill(pid, signal.SIGTERM)
90    print 'Server process killed'
Note: See TracBrowser for help on using the repository browser.