Python web programming CGI, pythonwebcgi
CGI (Common Gateway Interface) is the specification of external programs when the Web server is running. programs compiled by CGI can expand the server functions.
CGI applications can interact with browsers, and communicate with external data sources such as database servers through database APIs to obtain data from database servers.
After Formatting As an HTML document, send it to the browser. You can also put the data obtained from the browser into the database. Almost all servers support CGI and can write CGI in any language.
Configure apache to support the. py file. I am using the phpstudy integrated environment. I don't know what's going on. In windows, I didn't report any error in this slash,If you are using linux, follow the traffic rules (do not hit the wrong slash)
A simple Web form can be seen in HTML code. The form includes two input variables: user and age. The values of these two fields will be uploaded to the CGI script pe_info.py:
File Name: pe_info.html
<! DOCTYPE html>
File Name: pe_info.py
#! C: \ Users \ smelond \ AppData \ Local \ Programs \ Python \ Python36-32 \ python.exe # _ * _ coding: UTF-8 _ * _ # File_type: Return personal information # Filename: pe_info.py # Author: smelondimport cgiimport cgitb # If you want to see the tracing information of the Web application in the browser rather than "internal server error", you can use the cgitb module cgitb. enable () form = cgi. fieldStorage () # FieldStorage instance, which contains the print ("Content-Type: text/html \ n") values of the user and howmany fields # Note: if this is not found, A 500 error may be prompted: print ("<title> hello py </title>") if not 'user' in form: print ("no input name") else: user = form ["user"]. value # Get the value age = form ["age"] in the variable. value print ("
Note that in the first line, I use the absolute path of python3, instead #! /Usr/bin/env python; otherwise, Error 500 may occur.
Effect:
After submission:
Attackers can bypass the above static file and merge the two files into one file. The final script can use the dynamically generated HTML file to output the form and result pages, and know when to output the page.
#! C: \ Users \ anjing \ AppData \ Local \ Programs \ Python \ Python36-32 \ python.exe # _ * _ coding: UTF-8 _ * _ # File_type: submit personal information returned on a single page # Filename: pe_info1.py # Author: smelondimport cgiimport cgitbcgitb. enable () print ("Content-Type: text/html \ n ") formhtml = "