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

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

BROKEN!

I'm in the middle of implementing a logging callback in the server.

  • 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))
12DATADIR = '%s/test/data' % HOME
13VERSION = open('%s/VERSION' % HOME).read()
14NDG_MESSAGE_LEN = 256
15
16NDG_HANDSHAKE = "NDG-Delivery-server %s" % VERSION
17
18import logging
19logging.basicConfig(level=logging.DEBUG)
20
21import ndg.delivery.server.pybbftp 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
76# Assuming logging
77sys.exit(0)
78
79logfile = './bbftpd.log'
80n = len(open(logfile).readlines())
81pid = server.start(AuthHandler(), ['-l', 'DEBUG'])
82print 'Server process started at pid = %d' % pid
83print 'Monitoring %s' % logfile
84
85try:
86    while 1:
87        newlines = open(logfile).readlines()[n:]
88        n += len(newlines)
89        for line in newlines:
90            print line,
91        time.sleep(2)
92except:
93    os.kill(pid, signal.SIGTERM)
94    print 'Server process killed'
Note: See TracBrowser for help on using the repository browser.