SocketServer简单实现实时查看网站pv,uv
代码:
#!/usr/bin/env python # __*__ coding:utf8 __*__ import SocketServer import os class MyServer(SocketServer.BaseRequestHandler): logname= "/opt/nginx/logs/www.xxx.com_access.log" def handle(self): self.pv = os.popen("awk ‘{print $1}‘ %s|wc -l" % self.logname).read() self.uv = os.popen("awk ‘{print $1}‘ %s|sort|uniq -u|wc -l" % self.logname).read() print "pv-->",self.pv print "uv-->",self.uv conn = self.request result = """HTTP/1.0 200 OK <html> <a href="http://www.baidu.com">http://www.xxx.com</a> <br> PV : %s <br> UV : %s </html>""" % (self.pv,self.uv) print conn conn.recv(1024) conn.sendall(result) conn.close() if __name__ == "__main__": server = SocketServer.ThreadingTCPServer(("222.112.3.6",9191),MyServer) server.serve_forever()
在游览器请求的结果:
http://www.xxx.com
PV : 554
UV : 392
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。