-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
60 lines (51 loc) · 1.73 KB
/
index.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
##
## filename: index.py
##
## this is a wrapper for your main program, its primary purpose is to
## manage error messages and output your HTML, XML or JSON as required
##
from flask import Flask, request, make_response
application = Flask(__name__)
import traceback
import os
import sys
CURRENTDIR = os.path.dirname(os.path.abspath(__file__))
if CURRENTDIR not in sys.path:
sys.path.append(CURRENTDIR)
def wsErrorTextHTML(txt):
## displays the python error on the web page - useful for debugging
## but on a live system would have it save the error to a database and give the user an error number
err = "<p>ERROR!</p>"
err += "<pre>"+ txt + "</pre>"
return err
@application.errorhandler(500)
def internalServerError(error):
err = "<p>ERROR! 500</p>"
err += "<pre>"+ str(error) + "</pre>"
err += "<pre>"+ str(traceback.format_exc()) + "</pre>"
return err
@application.route('/', methods=['POST', 'GET'])
def indexApp():
try:
##
## ...your code goese here...
## I normally create a seperate 'core' application, in this case tempCore.py
##
from tempCore import TempCore
tempCore = TempCore()
htmlData = tempCore.index(request)
## output your html code
response = make_response(htmlData)
response.headers['Content-Type'] = 'text/html'
return response
except:
## for when you get an error message
response = make_response(wsErrorTextHTML(traceback.format_exc()))
response.headers['Content-Type'] = 'text/html'
return response
response = make_response("unkown!")
response.headers['Content-Type'] = 'text/html'
return response
if __name__ == "__main__":
application.debug = True
application.run()