目標服務到客戶端的顯示數據從服務器中的wxPython GUI客戶端尾-f登錄服務器,處理數據,然後通過捻
新人到扭曲。我有一個在Windows 7客戶端上運行的wxPython GUI,並且我有一個運行在產生日誌的Ubuntu服務器上的程序。我目前的嘗試是對日誌進行尾部處理,將輸出傳遞給扭曲的服務器,然後向客戶端提供符合我的正則表達式條件的任何數據。我已經打開了一個隧道,所以我不需要使SSH與事情複雜化。我已經得到了下面的代碼塊,但它僅用於輸入中的第一行。我知道我需要不斷檢查輸入的換行符,然後將其寫入傳輸,但我不知道如何在不中斷連接的情況下如何執行此操作。
我一直無法找到足夠的信息來將完整的解決方案拼湊在一起。我也嘗試過使用套接字和文件IO的各種其他方法,但我認爲Twisted似乎是解決此問題的好工具。我在正確的軌道上嗎?任何建議的讚賞。謝謝
#! /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()
要回答第一條評論,我也試着從Python中讀取日誌,程序掛起。代碼如下:
#! /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()
您是否考慮過從python內讀取日誌,而不是從'tail'輸出管道? – Velociraptors
已嘗試使用生成器來處理此問題,程序掛起。在我看來,運輸工具和發電機正在等待另一方完成。上面顯示的代碼。有沒有比使用發生器更好的方法來做到這一點? – jsucsy