Note that there are some explanatory texts on larger screens.

plurals
  1. POTail -f log on server, process data, then serve to client via twisted
    primarykey
    data
    text
    <p>Goal: Show data from server in wxPython GUI on client</p> <p>Newcomer to Twisted. I have a wxPython GUI running on a Windows 7 client, and I have a program running on an Ubuntu server that produces a log. My current attempt is to tail -f the log, pipe the output to a twisted server, then serve any data that meets my regex conditions to the client. I already have a tunnel open, so I don't need to complicate things with SSH. I've gotten the following block of code running, but it only serves the first line in the input. I know I need to keep checking the input for a newline and then writing it to the transport, but I'm not sure how to do that without breaking the connection. </p> <p>I haven't been able to find enough information to patch a full solution together. I have also tried various other methods using sockets and file IO, but I think Twisted seems to be a good tool for this issue. Am I on the right track? Any recommendations appreciated. Thanks</p> <pre><code>#! /usr/bin/python import optparse, os, sys from twisted.internet.protocol import ServerFactory, Protocol def parse_args(): usage = """usage: %prog [options] """ parser = optparse.OptionParser(usage) help = "The port to listen on. Default to a random available port." parser.add_option('--port', type='int', help=help) help = "The interface to listen on. Default is localhost." parser.add_option('--iface', help=help, default='localhost') options =parser.parse_args() return options#, log_file class LogProtocol(Protocol): def connectionMade(self): for line in self.factory.log: self.transport.write(line) class LogFactory(ServerFactory): protocol = LogProtocol def __init__(self,log): self.log = log def main(): log = sys.stdin.readline() options, log_file = parse_args() factory = LogFactory(log) from twisted.internet import reactor port = reactor.listenTCP(options.port or 0, factory, interface=options.iface) print 'Serving %s on %s.' % (log_file, port.getHost()) reactor.run() if __name__ == '__main__': main() </code></pre> <p>To answer the first comment, I have also tried to just read the log from within Python, program hangs. Code follows:</p> <pre><code>#! /usr/bin/python import optparse, os, sys, time from twisted.internet.protocol import ServerFactory, Protocol def parse_args(): usage = """ usage: %prog [options]""" parser = optparse.OptionParser(usage) help = "The port to listen on. Default to a random available port" parser.add_option('--port', type='int', help=help, dest="port") help = "The logfile to tail and write" parser.add_option('--file', help=help, default='log/testgen01.log',dest="logfile") options = parser.parse_args() return options class LogProtocol(Protocol): def connectionMade(self): for line in self.follow(): self.transport.write(line) self.transport.loseConnection() def follow(self): while True: line = self.factory.log.readline() if not line: time.sleep(0.1) continue yield line class LogFactory(ServerFactory): protocol = LogProtocol def __init__(self,log): self.log = log def main(): options, log_file = parse_args() log = open(options.logfile) factory = LogFactory(log) from twisted.internet import reactor port = reactor.listenTCP(options.port or 0, factory) #,interface=options.iface) print 'Serving %s on %s.' % (options.logfile, port.getHost()) reactor.run() if __name__ == '__main__': main() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload