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

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

Added example client and server executables. Small bug fix.

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