1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from SocketServer import BaseRequestHandler, ThreadingTCPServer from time import sleep import sys, socket from webloglib import log_fields, hit_tag class WebLogHandler(BaseRequestHandler): def handle(self): print"Connected from", self.client_address self.request.sendall('<hits>') try: while True: for hit in LOG.readlines(): self.request.sendall(hit_tag % log_fields(hit)) sleep(5) except socket.error: self.request.close() print"Disconnected from", self.client_address if __name__=='__main__': global LOG LOG = open('access-log') LOG.seek(0, 2) # Start at end of current access log srv = ThreadingTCPServer(('',8888), WebLogHandler) srv.serve_forever() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from twisted.internet import reactor from twisted.internet.protocol import Protocol, Factory from webloglib import hit_tag, log_fields class WebLog(Protocol): def connectionMade(self): print"Connected from", self.transport.client self.transport.write('<hits>') def dataReceived(self, data): newhits = LOG.readlines() ifnot newhits: self.transport.write('<none/>') for hit in newhits: self.transport.write(hit_tag % log_fields(hit)) def connectionLost(self, reason): print"Disconnected from", self.transport.client factory = Factory() factory.protocol = WebLog if __name__=='__main__': global LOG LOG = open('access-log') LOG.seek(0, 2) # Start at end of current access log reactor.listenTCP(8888, factory) reactor.run() |
1 2 3 4 5 | while 1: xml_data = sock.recv(8192) parser.feed(xml_data) sleep(5) # Delay before requesting new records sock.send('NEW?') # Send signal to indicate readiness |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |