码迷,mamicode.com
首页 > Web开发 > 详细

SocketServer简单实现实时查看网站pv,uv

时间:2015-04-08 19:58:20      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:socketserver http 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 

SocketServer简单实现实时查看网站pv,uv

标签:socketserver http pv uv

原文地址:http://dihuo.blog.51cto.com/1657091/1630134

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!