87 lines
2.2 KiB
Python
87 lines
2.2 KiB
Python
import cgi
|
|
import datetime
|
|
import workers.conn
|
|
import workers.val
|
|
|
|
def application(env, re):
|
|
if env["REQUEST_METHOD"] == "POST":
|
|
args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True)
|
|
elif env["REQUEST_METHOD"] == "GET":
|
|
args = cgi.parse_qs(env['QUERY_STRING'], True)
|
|
else:
|
|
re("405 Method Not Allowed", [])
|
|
return
|
|
|
|
try:
|
|
rtype = args["type"][0]
|
|
except:
|
|
re("400 Bad Request", [])
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
yield "A request type must be provided.".encode()
|
|
return
|
|
|
|
if rtype.lower() in ["conn", "connection"]:
|
|
try:
|
|
frm = args["from"][0]
|
|
to = args["to"][0]
|
|
|
|
if not frm or not to:
|
|
raise ValueError()
|
|
|
|
except:
|
|
re("400 Bad Request", [])
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
yield "\"from\" and \"to\" values are required for this type of request.".encode()
|
|
return
|
|
|
|
count = args["count"][0] if "count" in args and args["count"] else 3
|
|
date = args["date"][0] if "date" in args and args["date"] else None
|
|
time = args["time"][0] if "time" in args and args["time"] else None
|
|
mode = args["mode"][0] if "mode" in args and args["mode"] else None
|
|
|
|
try:
|
|
outtime = datetime.strptime("%s %s" % (date, time), "%d.%m.%Y %H:%M")
|
|
except:
|
|
re("400 Bad Request", [])
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
yield "The \"date\" value must be in DD.MM.YYYY format, the \"time\" value must be in HH:MM format."
|
|
return
|
|
|
|
try:
|
|
output = workers.conn.worker(frm, to, count, date, time, mode)
|
|
|
|
except Exception as e:
|
|
re("500 Internal Server Error", [])
|
|
yield "<h1>500 Internal Server Error</h1>".encode()
|
|
if "debug" in args:
|
|
yield str(e).encode()
|
|
return
|
|
|
|
re("200 OK", [])
|
|
return output.encode()
|
|
|
|
elif rtype.lower() in ["val", "validate"]:
|
|
try:
|
|
name = args["name"]
|
|
|
|
if not name:
|
|
raise ValueError()
|
|
|
|
except:
|
|
re("400 Bad Request", [])
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
yield "A \"name\" value is required for this type of request.".encode()
|
|
return
|
|
|
|
try:
|
|
output = workers.val.worker(name)
|
|
|
|
except:
|
|
re("500 Internal Server Error", [])
|
|
yield "<h1>500 Internal Server Error</h1>".encode()
|
|
if "debug" in args:
|
|
yield str(e).encode()
|
|
return
|
|
|
|
re("200 OK", [])
|
|
return output.encode()
|