21.2. cgi CGI ()

: Lib/cgi.py


(CGI)

Python CGI

21.2.1.

CGI HTTP HTML <FORM> <ISINDEX>

CGI cgi-bin HTTP ( URL)

URL Python ()

CGI 2 Python :

print("Content-Type: text/html")    # HTML is following
print()                             # blank line, end of headers

HTML HTML Python :

print("<TITLE>CGI script output</TITLE>")
print("<H1>This is my first CGI script</H1>")
print("Hello, world!")

21.2.2. cgi

import cgi

:

import cgitb
cgitb.enable()

:

import cgitb
cgitb.enable(display=0, logdir="/path/to/logdir")

cgitb cgitb

FieldStorage ASCII encoding HTML HEAD META Content-Type ( CGI )

FieldStorage Python in keys() len() FieldStorage keep_blank_values true

(Content-Type ) name addr :

form = cgi.FieldStorage()
if "name" not in form or "addr" not in form:
    print("<H1>Error</H1>")
    print("Please fill in the name and addr fields.")
    return
print("<p>name:", form["name"].value)
print("<p>addr:", form["addr"].value)
...further form processing here...

form[key] FieldStorage ( MiniFieldStorage ) value getvalue() getvalue() 2

form[key] FieldStorage MiniFieldStorage form.getvalue(key) (HTML ) getlist() ():

value = form.getlist("username")
usernames = ",".join(value)

value getvalue() filename file FieldStorage file (read() readline() ):

fileitem = form["userfile"]
if fileitem.file:
    # It's an uploaded file; count lines
    linecount = 0
    while True:
        line = fileitem.file.readline()
        if not line: break
        linecount = linecount + 1

FieldStorage with with

( submit ) done -1

( multipart/* ) FieldStorage type multipart/form-data ( multipart/* MIME )

( application/x-www-form-urlencoded ) MiniFieldStorage list file filename None

POST FieldStorage MiniFieldStorage

3.4 : file FieldStorage

3.5 : FieldStorage

21.2.3.

CGI FieldStorage

2

:

item = form.getvalue("item")
if isinstance(item, list):
    # The user is requesting more than one item.
else:
    # The user is requesting only one item.

:

<input type="checkbox" name="item" value="1" />
<input type="checkbox" name="item" value="2" />

:

user = form.getvalue("user").upper()

user=foo getvalue("user") upper() () AttributeError

getfirst() getlist()

FieldStorage.getfirst(name, default=None)

name 1 [1] default None

FieldStorage.getlist(name)

name name

:

import cgi
form = cgi.FieldStorage()
user = form.getfirst("user", "").upper()    # This way it's safe.
for item in form.getlist("item"):
    do_something(item)

21.2.4.

CGI

cgi.parse(fp=None, environ=os.environ, keep_blank_values=False, strict_parsing=False)

( sys.stdin ) keep_blank_values strict_parsing urllib.parse.parse_qs()

cgi.parse_qs(qs, keep_blank_values=False, strict_parsing=False)

urllib.parse.parse_qs()

cgi.parse_qsl(qs, keep_blank_values=False, strict_parsing=False)

urllib.parse.parse_qsl()

cgi.parse_multipart(fp, pdict)

() multipart/form-data fp Content-Type pdict

urllib.parse.parse_qs() FieldStorage

FieldStorage

cgi.parse_header(string)

(Content-Type ) MIME

cgi.test()

CGI HTTP HTML

cgi.print_environ()

HTML

cgi.print_form(form)

HTML

cgi.print_directory()

HTML

cgi.print_environ_usage()

(CGI ) HTML

cgi.escape(s, quote=False)

s '&''<' '>' HTML quote (") ; <a href="..."> HTML

3.2 : quote false deprecated html.escape()

21.2.5.

: ( os.system() os.popen() ) Web CGI URL CGI

21.2.6. CGI Unix

HTTP CGI ; cgi-bin

"others" ; Unix 8 0o755 (chmod 0755 filename ) 1 #! Python :

#!/usr/local/bin/python

Python "others"

"others" 0o644 0o666 HTTP "nobody" () () (PATH) Python (PYTHONPATH)

Python :

import sys
sys.path.insert(0, "/usr/home/joe/lib/python")
sys.path.insert(0, "/usr/local/lib/python")

()

Unix ; HTTP ( CGI )

21.2.7. CGI

CGI : Python HTTP

21.2.8. CGI

CGI (cgi.py) CGI HTML cgi-bin URL :

http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home

404 ( "addr" "At Home" "name" "Joe Blow") cgi.py

cgi test() : 1

cgi.test()

cgi.py

Python (: )Python Python CGI HTTP

cgitb 2:

import cgitb
cgitb.enable()

;

cgitb () :

import sys
sys.stderr = sys.stdout
print("Content-Type: text/plain")
print()
...your code here...

Python HTML HTML 2 HTML

21.2.9.

  • HTTP CGI
  • HTTP ( tail -f logfile )
  • python script.py
  • import cgitb; cgitb.enable()
  • PATH CGI
  • CGI userid : userid Web userid Web suexec userid
  • CGI set-uid

[1] HTML HTTP