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
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.
12
class hostCheckingValidator(Pyro.protocol.DefaultConnValidator):
14
Pyro.protocol.DefaultConnValidator.__init__(self)
15
def acceptHost(self,daemon,conn):
18
hostname=getHostname(ip)
21
print '\nNew connection from',ip,hostname
22
a=raw_input('Do you want to accept this? y/n: ')
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)
28
Pyro.util.Log.msg('ConnValidator','User denied connection from',ip,hostname)
29
return (0,Pyro.constants.DENIED_HOSTBLOCKED) # not ok
34
class testobject(Pyro.core.ObjBase):
36
print 'method was called with',arg
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'
49
# Construct the Pyro Daemon with our own connection validator
50
daemon = Pyro.core.Daemon()
51
daemon.setNewConnectionValidator(hostCheckingValidator()) # <-- mind this!!!
54
locator = Pyro.naming.NameServerLocator()
55
print 'searching for Naming Service...'
58
print 'Naming Service found at',ns.URI.address,'('+(Pyro.protocol.getHostname(ns.URI.address) or '??')+') port',ns.URI.port
60
# make sure our namespace group exists
61
try: ns.createGroup(':test')
62
except NamingError: pass
64
daemon.useNameServer(ns)
66
# connect a new object implementation (first unregister previous one)
67
try: ns.unregister('denyhosts')
68
except NamingError: pass
70
daemon.connect(testobject(),'denyhosts')
72
# enter the service loop.
73
print 'Server object "denyhosts" ready.'