~ubuntu-branches/debian/sid/pyro/sid

« back to all changes in this revision

Viewing changes to examples/denyhosts/server.py

  • Committer: Bazaar Package Importer
  • Author(s): Carl Chenet, Carl Chenet, Jakub Wilk
  • Date: 2010-09-14 01:04:28 UTC
  • Revision ID: james.westby@ubuntu.com-20100914010428-02r7p1rzr7jvw94z
Tags: 1:3.9.1-2
[Carl Chenet]
* revert to 3.9.1-1 package because of the development status 
  of the 4.1 package is unsuitable for stable use
  DPMT svn #8557 revision (Closes: #589172) 
* added debian/source
* added debian/source/format
* package is now 3.0 (quilt) source format
* debian/control
  - Bump Standards-Version to 3.9.1

[Jakub Wilk]
* Add ‘XS-Python-Version: >= 2.5’ to prevent bytecompilation with python2.4
  (closes: #589053).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import sys
 
3
import Pyro.naming, Pyro.core, Pyro.util, Pyro.protocol, Pyro.constants
 
4
from Pyro.errors import PyroError,NamingError
 
5
from Pyro.protocol import getHostname
 
6
 
 
7
 
 
8
######## Custom connections validator.
 
9
######## This validator asks the user to accept or deny a new connection.
 
10
######## Note that the old validator is still called when the user accepts.
 
11
 
 
12
class hostCheckingValidator(Pyro.protocol.DefaultConnValidator):
 
13
        def __init__(self):
 
14
                Pyro.protocol.DefaultConnValidator.__init__(self)
 
15
        def acceptHost(self,daemon,conn):
 
16
                (ip, port)=conn.addr
 
17
                try:
 
18
                        hostname=getHostname(ip)
 
19
                except:
 
20
                        hostname='<unknown>'
 
21
                print '\nNew connection from',ip,hostname
 
22
                a=raw_input('Do you want to accept this? y/n: ')
 
23
                if a=='y':
 
24
                        # user accepts, but pass it on to the default validator,
 
25
                        # which will check the max. number of connections...
 
26
                        return Pyro.protocol.DefaultConnValidator.acceptHost(self,daemon, conn)
 
27
                else:
 
28
                        Pyro.util.Log.msg('ConnValidator','User denied connection from',ip,hostname)
 
29
                        return (0,Pyro.constants.DENIED_HOSTBLOCKED)    # not ok
 
30
 
 
31
 
 
32
##### test object
 
33
 
 
34
class testobject(Pyro.core.ObjBase):
 
35
        def method(self,arg):
 
36
                print 'method was called with',arg
 
37
                return 42
 
38
 
 
39
 
 
40
##### main program.
 
41
 
 
42
# initialize the server and set the default namespace group
 
43
Pyro.core.initServer()
 
44
Pyro.config.PYRO_TRACELEVEL=3
 
45
Pyro.config.PYRO_NS_DEFAULTGROUP=':test'
 
46
Pyro.config.PYRO_LOGFILE='server_log'
 
47
print 'Check the logfile for messages: server_log'
 
48
 
 
49
# Construct the Pyro Daemon with our own connection validator
 
50
daemon = Pyro.core.Daemon()
 
51
daemon.setNewConnectionValidator(hostCheckingValidator())   # <-- mind this!!!
 
52
 
 
53
# locate the NS
 
54
locator = Pyro.naming.NameServerLocator()
 
55
print 'searching for Naming Service...'
 
56
ns = locator.getNS()
 
57
 
 
58
print 'Naming Service found at',ns.URI.address,'('+(Pyro.protocol.getHostname(ns.URI.address) or '??')+') port',ns.URI.port
 
59
 
 
60
# make sure our namespace group exists
 
61
try: ns.createGroup(':test')
 
62
except NamingError: pass
 
63
 
 
64
daemon.useNameServer(ns)
 
65
 
 
66
# connect a new object implementation (first unregister previous one)
 
67
try: ns.unregister('denyhosts')
 
68
except NamingError: pass
 
69
 
 
70
daemon.connect(testobject(),'denyhosts')
 
71
 
 
72
# enter the service loop.
 
73
print 'Server object "denyhosts" ready.'
 
74
daemon.requestLoop()
 
75