代码:
#!/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
原文地址:http://dihuo.blog.51cto.com/1657091/1630134