forked from b1tr0t/Google-Analytics-for-Mobile--python-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga_mobile_server.py
executable file
·107 lines (92 loc) · 3.03 KB
/
ga_mobile_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# encoding: utf-8
"""
server.py
Serves up google analytics 1x1.gif transparent tracking images and notifies Google Analytics of clicks.
Created by Peter McLachlan on 2009-07-19.
Copyright (c) 2009 Mobify. All rights reserved.
"""
import sys
import os
import getopt
from urlparse import urlparse
from flup.server.fcgi_fork import WSGIServer
from socket import gethostname
from datetime import datetime, timedelta
from ga import track_page_view
from messaging import stdMsg, dbgMsg, errMsg, setDebugging
setDebugging(1)
MINSPARE = 3
MAXSPARE = 7
MAXCHILDREN = 50
MAXREQUESTS = 500
HOST = '127.0.0.1'
PORT = 8009
PIDFILE = '/tmp/g_analytic_server.pid'
HELP_MESSAGE = """
This is some help.
"""
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def gserve(environ, start_response):
try:
response = track_page_view(environ)
except Exception, e:
print e
start_response("503 Service Unavailable", [])
return ["<h1>Exception loading GA code</h1><p>%s</p>" % str(e)]
start_response(response['response_code'], response['response_headers'])
return [response['response_body']]
def main(argv=None):
host = HOST
port = PORT
pidfile = PIDFILE
maxchildren = MAXCHILDREN
maxrequests = MAXREQUESTS
minspare = MINSPARE
maxspare = MAXSPARE
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "h", ["host=", "port=", 'pidfile=', 'maxchildren=',
'maxrequests=', 'minspare=', 'maxspare=', 'help'])
except getopt.error, msg:
raise Usage(msg)
# option processing
for option, value in opts:
if option in ("-h", "--help"):
raise Usage(HELP_MESSAGE)
elif "--host" == option:
host = value
elif "--port" == option:
port = int(value)
elif "--pidfile" == option:
pidfile = value
elif "--maxchildren" == option:
maxchildren = int(value)
elif "--maxrequests" == option:
maxrequests = int(value)
elif "--minspare" == option:
minspare = int(value)
elif "--maxspare" == option:
maxspare = int(value)
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, "\t for help use --help"
return -2
try:
f = open(pidfile, 'w')
f.write(str(os.getpid()) + '\n')
f.close()
except IOError, e:
print "!! Error writing to pid file, check pid file path: %s" % pidfile
return -1
try:
WSGIServer(gserve, bindAddress=(host, port), minSpare=minspare, maxSpare=maxspare, maxChildren=maxchildren, maxRequests=maxrequests).run()
except Exception, e:
print "!! WSGIServer raised exception"
print e
if __name__ == "__main__":
sys.exit(main())