All pastes #37579 Raw Edit

Service Multicast

public python v1 · immutable
#37579 ·published 2006-01-20 16:11 UTC
rendered paste body
# -*- conding: utf-8 -*-# -*- python-mode -*-## Serveur Web multicast pour le serveur de backup Adelux## Le repertoire local ./root/ contient les Systems Linux installables# Un System = fichier.tar.gz, data, scripts d'install permettant de# restaurer ce systeme Linux#from twisted.internet import reactorfrom twisted.python import logfrom twisted.internet.protocol import DatagramProtocol,ServerFactoryfrom twisted.internet.udp import MulticastMixinfrom twisted.application import internet, servicefrom twisted.web import vhost, staticfrom twisted.web import serverfrom twisted.web import xmlrpcimport xmlrpclib,sys,socket,re,urllib## Variables# Repertoire local (dans le repertoire ou se trouve ce script) dans lequel# se trouvent les Systems a restaurerserver_root = 'root'## Protocol de Multicastclass MulticasterProtocol(DatagramProtocol):    def datagramReceived(self, datagram, address):        """ We can choose to return an arbitrary ip address, or the local address        using the getLocalIPAddress(), or get the connected server ip address using        the special keyword 'me' """        #self.transport.write('twisted:%s' % getLocalIPAddress(),address)        self.transport.write('twisted:me',address)"""class MulticasterService(service.Service):    def __init__(self, *args, **kwargs):        self.parent.__init__(self, *args)    def getMulticasterFactory(self):        f = ServerFactory()        f.protocol = MulticasterProtocol        return f"""class Echoer(xmlrpc.XMLRPC):    def xmlrpc_echo(self, *args):        return args## Local functionsdef getLocalIPAddress():    return socket.gethostbyaddr(socket.gethostname())[2][0]def publicIPAddress():    # Change it to use getpage instead of urllib.urlopen    return re.findall('[0-9.]+', urllib.urlopen('http://checkip.dyndns.org/').read())[-1]## Mainif __name__ == '__main__':    a = service.Application('adelux', uid=0, gid=0)    serviceCollection = service.IServiceCollection(a)    log.startLogging(sys.stdout, 0)    # Creation du serveur Multicast    #s = MulticasterService('M')    s = MulticasterProtocol()    i = internet.MulticastServer(9999, s, "224.0.0.1")    i.setName('MulticastServer')    i.setServiceParent(serviceCollection)    # Definition of a static directory, in the current one, this is    # the root directory of the web server    root = static.File(server_root)    root.putChild('RPC2', Echoer())    site = server.Site(root)    i = internet.TCPServer(80, site)    i.setName('Web Server')    i.setServiceParent(serviceCollection)    # Go !    serviceCollection.startService()    reactor.run()application = service.Application('Adelux Multicast Server', uid=0, gid=0)serviceCollection = service.IServiceCollection( application )# Creation du serveur Multicast#s = MulticasterService('M')s = MulticasterProtocol()i = internet.MulticastServer(9999, s, "224.0.0.1")i.setName('MulticastServer')i.setServiceParent(serviceCollection)