-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpd.py
executable file
·133 lines (108 loc) · 3.06 KB
/
httpd.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Start application server on port 8080
The first command line argument will set the port to be bound. Remeber, you
need root privvileges to bind ports below 1024.
"""
import os, sys
#os.chdir(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib'))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib', 'web'))
import web, config, json, db
import datetime
import time
from sqlalchemy import *
import tpl
#from webctx import *
import webctx
import tempfile
from websession import WebSession
session_default = {
"selected_pid": 0,
"pid": None,
"user": None,
"isadmin": False,
"email": None,
"eid": None
}
def unloadhook():
db.session.flush()
db.connection.close()
def loadhook():
print datetime.datetime.now().strftime("%Y-%m-%d %H.%M.%S") + "\t" +\
web.ctx.method + "\t" + \
web.ctx.env.get('REQUEST_URI')
#init_session(app)
# allow to pass a custom port/ip into the application
class rot(web.application):
def run(self, port=8080, ip='0.0.0.0', *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, (ip, port))
import sys, logging
from wsgilog import WsgiLog
import config
class Log(WsgiLog):
def __init__(self, application):
WsgiLog.__init__(
self,
application,
logformat = '%(message)s',
tofile = True,
toprint = True,
file = config.app_logfile,
when = "D",
interval = 1,
backups = 1000
)
def init_session(app):
if web.config.get('_session') is None:
print "no session"
web.config.session_parameters['cookie_name'] = 'rot'
web.config.session_parameters['cookie_domain'] = None
web.config.session_parameters['timeout'] = config.session_timeout,
web.config.session_parameters['ignore_expiry'] = False
web.config.session_parameters['ignore_change_ip'] = False
web.config.session_parameters['secret_key'] = config.session_salt
web.config.session_parameters['expired_message'] = 'Session expired'
temp = tempfile.mkdtemp(dir=config.session_dir, prefix='session_')
web.debug("==> init web.session.Session()")
webctx.session = WebSession(
app,
web.session.DiskStore(temp),
initializer = session_default
)
#set_session(s)
else:
print "==> recycling session"
webctx.session = web.config._session
"""
try:
s["pid"]
except:
print "==> default session"
s = session_default
"""
#global session
#session = s
if __name__ == "__main__":
curdir = os.path.dirname(__file__)
curdir=""
web.config.debug = config.web_debug
#web.config.debug = False
app = rot(webctx.urls, globals())
init_session(app)
app.add_processor(web.loadhook(loadhook))
app.add_processor(web.unloadhook(unloadhook))
# web.myapp = app
# web.init_session = init_session
app.run(config.port, "0.0.0.0", Log)
else:
web.config.debug = config.web_debug
app = web.application(webctx.urls, globals())
init_session(app)
app.add_processor(web.loadhook(loadhook))
app.add_processor(web.unloadhook(unloadhook))
# web.myapp = app
# web.init_session = init_session
application = app.wsgifunc(Log)