就像QUERY_STRING
在运行 cgi 脚本时添加访问值的替代方法一样,您可以执行以下操作:
import os
print "content-type: text/html\n" # so we can print to the webpage
print os.environ['QUERY_STRING']
我的测试和理解是,当 URL 中没有任何查询字符串时,这也有效,你只会得到一个空字符串。
这已确认正在处理2.7.6
,查看所有环境变量,如下所示:
#!/usr/bin/python
import os
print "Content-type: text/html\r\n\r\n";
print "<font size=+1>Environment</font><\br>";
for param in os.environ.keys():
print "<b>%20s</b>: %s<\br>" % (param, os.environ[param])
这段代码是从关于使用 Python 进行 CGI 编程的TutorialsPoint 教程中获得的。
虽然,正如zombie_raptor_jesus 所提到的,使用 Python 的 CGI 模块可能会更好,使用 FieldStorage 会使事情变得更容易。
再次来自上面的教程:
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
将保存查询字符串中的值first_name=Bobby&last_name=Ray