-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
89 lines (78 loc) · 2.68 KB
/
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
import json
import web
import subprocess
import re
import socket
urls = (
'', 'fhunter',
'/', 'fhunter',
'/fortune', 'fortune',
)
#------------- STATIC --------------
BASE_URI = ''
SERVER_HOSTNAME = socket.gethostname()
if SERVER_HOSTNAME == 'matrix.nuchwezi':
BASE_URI = '/fhunter'
APP_TITLE = 'FORTUNE HUNTER'
#------------- DB -------------
db = web.database(dbn='DB', user='USER', pw='PASSWORD', db='fhunter', host='localhost')
def record_hit(hit):
new_id = db.insert('fhunter_hits',seqname='fhunter_hits_id_seq',
ip = hit['ip'],
score = hit['score'],
extra = hit['extra'],
method = hit['method'] )
return new_id
#--------- Templates ----------
render = web.template.render('/var/opt/fhunter/templates/', cache=False)
#------ utils------
def get_fortune():
return subprocess.Popen(['fortune'], stdout=subprocess.PIPE).communicate()[0]
#~~~~~~~~~~~~~~ VIEWS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SCORE_INCREMENT = 10
PASS_PENALTY = 5
class fhunter:
def POST(self):
params = web.input(score=0)
hit = {
'ip': web.ctx.ip,
'score' : params.score,
'method' : 'POST',
'extra' : json.dumps( web.ctx.environ.get('HTTP_REFERER','') )
}
record_hit( hit )
fortune = re.split('\s+',get_fortune())
return render.fhunter({
'APP_TITLE' : APP_TITLE,
'BASE_URI' : BASE_URI,
'fortune' : ' '.join( fortune ).strip(),
'score' : params.score or 0,
'SCORE_INCREMENT' : SCORE_INCREMENT * (( len(fortune) / 10 ) or 1),
'PASS_PENALTY' : PASS_PENALTY * (( len(fortune) / 10 ) or 1),
'continue' : True
})
def GET(self):
web.ctx.status = '200 OK'
web.header('Content-Type', 'text/html')
hit = {
'ip': web.ctx.ip,
'score' : None,
'method' : 'GET',
'extra' : json.dumps( web.ctx.environ.get('HTTP_REFERER','') )
}
record_hit( hit )
fortune = re.split('\s+',get_fortune())
return render.fhunter({
'APP_TITLE' : APP_TITLE,
'BASE_URI' : BASE_URI,
'fortune' : ' '.join( fortune ).strip(),
'score' : 0,
'SCORE_INCREMENT' : SCORE_INCREMENT * (( len(fortune) / 10 ) or 1),
'PASS_PENALTY' : PASS_PENALTY * (( len(fortune) / 10 ) or 1),
'continue' : False
})
application = web.application(urls, globals()).wsgifunc()
web.webapi.internalerror = web.debugerror
if __name__ == '__main__':
application = web.application(urls, globals())
application.run()