I want to ask about the error handling of cgi.FieldStorage() https://docs.python.org/2/library/cgi.html I have reference the exmaple to write my program below is my program
arg = cgi.FieldStorage() if "s_d" not in arg or "e_d" not in arg: print "no valid input" return else: s_date = arg["s_d"].value e_date = arg["e_d"].value print s_date print type(s_date) print e_date print type(e_date)
It works when I click below URI http: / /hostname/test.py?s_d=%272015-01-01%27&e_d=%272015-02-01%27
When I want to try the error handling, it failed http: / /hostname/test.py
The error log is like below
File "test.py", line 152 return SyntaxError: 'return' outside function Premature end of script headers: test.py
I want to protect my program. Make it can go with no parameter or whatever parameters. Please advice. many thanks
yes , thanks for @Forge 's suggestion I write my program again, it is cool now.
arg = cgi.FieldStorage() s_date = '' e_date = '' def get_parameter(): global s_date,e_date if "s_d" not in arg or "e_d" not in arg: print "no valid input" return else: s_date = arg["s_d"].value e_date = arg["e_d"].value get_parameter()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.