how to monitor a website traffic python

Solutions on MaxInterview for how to monitor a website traffic python by the best coders in the world

showing results for - "how to monitor a website traffic python"
Blossom
16 Oct 2020
1    from flask import Flask, render_template, request, session, jsonify
2    import urllib.request
3    from pusher import Pusher
4    from datetime import datetime
5    import httpagentparser
6    import json
7    import os
8    import hashlib
9    from dbsetup import create_connection, create_session, update_or_create_page, select_all_sessions, select_all_user_visits, select_all_pages
10
11    app = Flask(__name__)
12    app.secret_key = os.urandom(24)
13
14    # configure pusher object
15    pusher = Pusher(
16    app_id='PUSHER_APP_ID',
17    key='PUSHER_APP_KEY',
18    secret='PUSHER_APP_SECRET',
19    cluster='PUSHER_APP_CLUSTER',
20    ssl=True)
21
22    database = "./pythonsqlite.db"
23    conn = create_connection(database)
24    c = conn.cursor()
25
26    userOS = None
27    userIP = None
28    userCity = None
29    userBrowser = None
30    userCountry = None
31    userContinent = None
32    sessionID = None
33
34    def main():
35        global conn, c
36
37    def parseVisitor(data):
38        update_or_create_page(c,data)
39        pusher.trigger(u'pageview', u'new', {
40            u'page': data[0],
41            u'session': sessionID,
42            u'ip': userIP
43        })
44        pusher.trigger(u'numbers', u'update', {
45            u'page': data[0],
46            u'session': sessionID,
47            u'ip': userIP
48        })
49
50    @app.before_request
51    def getAnalyticsData():
52        global userOS, userBrowser, userIP, userContinent, userCity, userCountry,sessionID 
53        userInfo = httpagentparser.detect(request.headers.get('User-Agent'))
54        userOS = userInfo['platform']['name']
55        userBrowser = userInfo['browser']['name']
56        userIP = "72.229.28.185" if request.remote_addr == '127.0.0.1' else request.remote_addr
57        api = "https://www.iplocate.io/api/lookup/" + userIP
58        try:
59            resp = urllib.request.urlopen(api)
60            result = resp.read()
61            result = json.loads(result.decode("utf-8"))                                                                                                     
62            userCountry = result["country"]
63            userContinent = result["continent"]
64            userCity = result["city"]
65        except:
66            print("Could not find: ", userIP)
67        getSession()
68
69    def getSession():
70        global sessionID
71        time = datetime.now().replace(microsecond=0)
72        if 'user' not in session:
73            lines = (str(time)+userIP).encode('utf-8')
74            session['user'] = hashlib.md5(lines).hexdigest()
75            sessionID = session['user']
76            pusher.trigger(u'session', u'new', {
77                u'ip': userIP,
78                u'continent': userContinent,
79                u'country': userCountry,
80                u'city': userCity,
81                u'os': userOS,
82                u'browser': userBrowser,
83                u'session': sessionID,
84                u'time': str(time),
85            })
86            data = [userIP, userContinent, userCountry, userCity, userOS, userBrowser, sessionID, time]
87            create_session(c,data)
88        else:
89            sessionID = session['user']
90
91    @app.route('/')
92    def index():
93        data = ['home', sessionID, str(datetime.now().replace(microsecond=0))]
94        parseVisitor(data)
95        return render_template('index.html')
96
97    @app.route('/about')
98    def about():
99        data = ['about',sessionID, str(datetime.now().replace(microsecond=0))]
100        parseVisitor(data)
101        return render_template('about.html')
102
103    @app.route('/dashboard')
104    def dashboard():
105        return render_template('dashboard.html')
106
107    @app.route('/dashboard/<session_id>', methods=['GET'])
108    def sessionPages(session_id):
109        result = select_all_user_visits(c,session_id)
110        return render_template("dashboard-single.html",data=result)
111
112    @app.route('/get-all-sessions')
113    def get_all_sessions():
114        data = []
115        dbRows = select_all_sessions(c)
116        for row in dbRows:
117            data.append({
118                'ip' : row['ip'],
119                'continent' : row['continent'],
120                'country' : row['country'], 
121                'city' : row['city'], 
122                'os' : row['os'], 
123                'browser' : row['browser'], 
124                'session' : row['session'],
125                'time' : row['created_at']
126            })
127        return jsonify(data)
128
129    if __name__ == '__main__':
130        main()
131        app.run(debug=True)