# -*- 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 reactor from twisted.python import log from twisted.internet.protocol import DatagramProtocol,ServerFactory from twisted.internet.udp import MulticastMixin from twisted.application import internet, service from twisted.web import vhost, static from twisted.web import server from twisted.web import xmlrpc import xmlrpclib,sys,socket,re,urllib ## Variables # Repertoire local (dans le repertoire ou se trouve ce script) dans lequel # se trouvent les Systems a restaurer server_root = 'root' ## Protocol de Multicast class 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 functions def 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] ## Main if __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)