Commit 532ced09 authored by Romain Loth's avatar Romain Loth

better exception handling in cgi

parent 58b05e9a
......@@ -10,7 +10,8 @@ __version__ = "1"
__email__ = "romain.loth@iscpif.fr"
__status__ = "Test"
from cgi import FieldStorage
from cgi import FieldStorage
from traceback import format_tb
# debug
import cgitb
......@@ -21,51 +22,60 @@ if __name__ == "__main__":
# reception: the cgi library gets vars from html form within received http POST
this_data = FieldStorage()
# read into local str vars
first_name = this_data['first_name'].value
middle_name = this_data['middle_name'].value
last_name = this_data['last_name'].value
initials = this_data['initials'].value
email = this_data['email'].value
country = this_data['country'].value
jobtitle = this_data['hon_title'].value
keywordsss = this_data['keywords'].value # single string but ','-separated
# keywordzzz = this_data.getlist(keywords) # array
# fyi actual form fields were
# ['email', 'password', 'password2',
# 'hon_title', 'first_name', 'middle_name', 'last_name', 'initials',
# 'keywords', 'country', 'my-captcha', 'my-captchaHash']
# todo
# institution = this_data[].value
try:
# read into local str vars
first_name = this_data['first_name'].value
middle_name = this_data['middle_name'].value
last_name = this_data['last_name'].value
initials = this_data['initials'].value
email = this_data['email'].value
country = this_data['country'].value
jobtitle = this_data['hon_title'].value
keywordsss = this_data['keywords'].value # single string but ','-separated
# keywordzzz = this_data.getlist(keywords) # array
# optional
# picture = form["user_picture"]
# if picture.file & picture.filename:
# picture_bytes = picture.value
# todo
# institution = this_data[].value
# debug log
# log = open('../cgi_my_output.log', 'w')
# print('first_name:',first_name, file=log)
# print('midle_name:',midle_name, file=log)
# print('last_name:',last_name, file=log)
# print('initials:',initials, file=log)
# print('email:',email, file=log)
# print('country:',country, file=log)
# print('jobtitle:',jobtitle, file=log)
# print('keywordsss:',keywordsss, file=log)
# print('instituton:',institution, file=log)
# log.close()
# optional
# picture = form["user_picture"]
# if picture.file & picture.filename:
# picture_bytes = picture.value
# response
print("Content-type: text/html")
print() # blank line <=> end of headers
# response
print("Content-type: text/html")
print() # blank line <=> end of headers
print("<TITLE>CGI script output</TITLE>")
print([k for k in this_data])
#
print('<br>first_name:',first_name)
print('<br>midle_name:',middle_name)
print('<br>last_name:',last_name)
print('<br>initials:',initials)
print('<br>email:',email)
print('<br>country:',country)
print('<br>jobtitle:',jobtitle)
print('<br>keywords:',keywordsss)
# print('instituton:',institution)
print("<TITLE>CGI script output</TITLE>")
print([k for k in this_data])
#
print('<br>first_name:',first_name)
print('<br>midle_name:',middle_name)
print('<br>last_name:',last_name)
print('<br>initials:',initials)
print('<br>email:',email)
print('<br>country:',country)
print('<br>jobtitle:',jobtitle)
print('<br>keywords:',keywordsss)
# print('instituton:',institution)
except KeyError as kerrr:
print("Content-type: text/html")
print() # blank line <=> end of headers
print("<h3>Your form was empty</h3")
print("<p style='font-family:monospace; font-size:80%'")
print("<br/>".join(format_tb(kerrr.__traceback__)))
print("</p>")
except Exception as errr:
print("Content-type: text/html")
print() # blank line <=> end of headers
print("<h3>There was an error:</h3")
print("<p style='font-family:monospace; font-size:80%'")
print("<br/>".join(format_tb(errr.__traceback__)))
print("</p>")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment