it is in the email.<br><br>#Try to build a Twisted Connector Service the twisted way<br>from twisted.application import internet, service<br>from twisted.web import resource, server, static, xmlrpc
<br>import re, time<br><br><br>from EventTypes import Event<br>connectiontypes = {'TelnetConnection':'A telnet client connection.', 'FileTailConnection':'Facilitat<div style="direction: ltr;">es tailing a log file'}<br><br>
<br>class Connection:<br> def __init__(self,name):
<br> # Constructor<br> self.name=name<br> #print "Connection name: %s" % <a href="http://self.name/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">self.name</a><br>
self.status=False<br><br> def setStatus (self,status):<br> self.status=status
<br><br>####################<br># TCP Client Stuff<br>####################<br>from twisted.internet.protocol import Protocol, ReconnectingClientFactory<br>from twisted.conch.telnet import Telnet<br><br><br>##########################
<br># Telnet Clients<br>##########################<br>class TelnetClient(Telnet):<br> "TelnetClient class." <br> def connectionMade(self):<br> #print "connection made to %s" % <a href="http://self.factory.name/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
self.factory.name</a><br> #if the cmd variable was set in the calling factory, lets iterate over it.<br> if self.factory.cmd:<br> for cmd in self.factory.cmd:<br> if "sleep" in cmd:<br> #print
re.compile('(?P<sleeptime>(\d+))').search(cmd).group('sleeptime')<br> sleeptime = int(re.compile('(?P<sleeptime>(\d+))').search(cmd).group('sleeptime'))<br> time.sleep(sleeptime)
<br>
else:<br> self.write(cmd)<br> <br><br> def write(self, data):<br> #print data<br> Telnet._write(self, data+"\r\n")<br><br><br> def dataReceived(self, data):<br> print "received:", data
<br> #always send data to the eventQueue<br> self.factory.eventQueue.insert(Event(data, <a href="http://self.factory.name/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">self.factory.name</a>
))<br> #check if we should send any commands based on data received<br>
if self.factory.expect:<br> #check if any of the expect dictionary keys are in data<br> for key in self.factory.expect.keys(): <br> if key in data:<br> for cmd in self.factory.expect
[key]:<br> if "sleep" in cmd:<br> sleeptime = int(re.compile('(?P<sleeptime>(\d+))').search(cmd).group('sleeptime'))<br> time.sleep(sleeptime)<br>
else:<br> self.write(cmd)<br> <br><br><br>class TelnetConnection(Connection, ReconnectingClientFactory):<br> "Telnets to host:port and executes cmd. cmd"
<br>
protocol = TelnetClient<br> def __init__(self, name, eventQueue, cmd=None, expect=None): <br> Connection.__init__(self, name)<br> self.cmd = cmd<br> self.expect = expect<br> self.eventQueue = eventQueue
<br> <br><br> <br><br> def clientConnectionFailed(self, connector, reason):<br> print 'connection failed:', reason.getErrorMessage()<br> <br><br> def clientConnectionLost(self, connector, reason):
<br> print 'connection lost:', reason.getErrorMessage()<br><br><br>class ManagerService(service.Service):<br> def __init__(self):<br> pass<br><br> # some XML-RPC function calls for twisted server<br> def stop(self):
<br> return 'Shutdown initiated'<br><br> def getConnectionTypes(self):<br> """Return connection types"""<br> return connectiontypes<br><br> def xmlrpc_getActiveConnections(self):
<br> """Return all active connections"""<br><br> def getConnectionStatus(self, connection):<br> return "connection status"<br><br><br> def getResource(self):<br>
"""Return sum of arguments."""<br> r = resource.Resource()<br> x = xmlrpc.XMLRPC()<br> x.xmlrpc_stop = self.stop<br> x.xmlrpc_getConnectionStatus = self.getConnectionStatus
<br> x.xmlrpc_startTelnet = self.startTelnet<br> <br> r.putChild('RPC2', x)<br> return r<br><br> def startTelnet(self, host, port, name, queue, command, script):<br> from twisted.internet import reactor
<br> from EventQueue import Queue<br> #Create the Event Queue object<br> self.queue = queue<br><br> self.queue = Queue()<br><br> reactor.connectTCP(host, int(port), TelnetConnection(name,self.queue
,command,script))<br> return "%s started" % name<br><br><br>#create an application instance<br>application = service.Application('connectorservice', uid=1, gid=1)<br>m = ManagerService()<br>serviceCollection =
service.IServiceCollection(application)<br>internet.TCPServer(7090, server.Site(m.getResource())<br> ).setServiceParent(serviceCollection)<br>
</div><br><br><div><span class="gmail_quote">On 3/4/06, <b class="gmail_sendername">Nikolaus Schlemm</b> <<a href="mailto:nikl@nikl.net">nikl@nikl.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
> Okay, I've made some huge progress, I think with my Telnet connection<br>> stuff<br>up to now just one thing: there is no sample tac ;)<br>--<br>MfG,<br><br> Nikl<br><br><br>_______________________________________________
<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br><a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
</a><br><br><br><br></blockquote></div><br>