<Directory "/var/www/cgi-bin"> AllowOverride None Options +ExecCGI Order allow,deny Allow from all </Directory>
print ("Content-type:text/html") print () # 空行,告诉服务器结束头部 print ('<html>') print ('<head>') print ('<meta charset="utf-8">') print ('<title>Hello Word - 我的第一个 CGI 程序!</title>') print ('</head>') print ('<body>') print ('<h2>Hello Word! 我是来自菜鸟教程的第一CGI程序</h2>') print ('</body>') print ('</html>')
import os print ("Content-type: text/html") print () print ("<meta charset="utf-8">") print ("<b>环境变量</b><br>") print ("<ul>") for key in os.environ.keys(): print ("<li><span style='color:green'>%30s </span> : %s </li>" % (key,os.environ[key])) print ("</ul>")
# CGI处理模块 import cgi, cgitb # 创建 FieldStorage 的实例化 form = cgi.FieldStorage() # 获取数据 site_name = form.getvalue('name') site_url = form.getvalue('url') print ("Content-type:text/html") print () print ("<html>") print ("<head>") print ("<meta charset="utf-8">") print ("<title>菜鸟教程 CGI 测试实例</title>") print ("</head>") print ("<body>") print ("<h2>%s官网:%s</h2>" % (site_name, site_url)) print ("</body>") print ("</html>")
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form action="/cgi-bin/hello_get.py" method="get"> 站点名称: <input type="text" name="name"> <br /> 站点 URL: <input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html>
# CGI处理模块 import cgi, cgitb # 创建 FieldStorage 的实例化 form = cgi.FieldStorage() # 获取数据 site_name = form.getvalue('name') site_url = form.getvalue('url') print ("Content-type:text/html") print () print ("<html>") print ("<head>") print ("<meta charset="utf-8">") print ("<title>菜鸟教程 CGI 测试实例</title>") print ("</head>") print ("<body>") print ("<h2>%s官网:%s</h2>" % (site_name, site_url)) print ("</body>") print ("</html>")
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form action="/cgi-bin/hello_get.py" method="post"> 站点名称: <input type="text" name="name"> <br /> 站点 URL: <input type="text" name="url" /> <input type="submit" value="提交" /> </form> </body> </html> </form>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form action="/cgi-bin/checkbox.py" method="POST" target="_blank"> <input type="checkbox" name="runoob" value="on" /> 菜鸟教程 <input type="checkbox" name="google" value="on" /> Google <input type="submit" value="选择站点" /> </form> </body> </html>
# 引入 CGI 处理模块 import cgi, cgitb # 创建 FieldStorage的实例 form = cgi.FieldStorage() # 接收字段数据 if form.getvalue('google'): google_flag = "是" else: google_flag = "否" if form.getvalue('runoob'): runoob_flag = "是" else: runoob_flag = "否" print ("Content-type:text/html") print () print ("<html>") print ("<head>") print ("<meta charset="utf-8">") print ("<title>菜鸟教程 CGI 测试实例</title>") print ("</head>") print ("<body>") print ("<h2> 菜鸟教程是否选择了 : %s</h2>" % runoob_flag) print ("<h2> Google 是否选择了 : %s</h2>" % google_flag) print ("</body>") print ("</html>")
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form action="/cgi-bin/radiobutton.py" method="post" target="_blank"> <input type="radio" name="site" value="runoob" /> 菜鸟教程 <input type="radio" name="site" value="google" /> Google <input type="submit" value="提交" /> </form> </body> </html>
# 引入 CGI 处理模块 import cgi, cgitb # 创建 FieldStorage的实例 form = cgi.FieldStorage() # 接收字段数据 if form.getvalue('site'): site = form.getvalue('site') else: site = "提交数据为空" print ("Content-type:text/html") print () print ("<html>") print ("<head>") print ("<meta charset="utf-8">") print ("<title>菜鸟教程 CGI 测试实例</title>") print ("</head>") print ("<body>") print ("<h2> 选中的网站是 %s</h2>" % site) print ("</body>") print ("</html>")
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form action="/cgi-bin/textarea.py" method="post" target="_blank"> <textarea name="textcontent" cols="40" rows="4"> 在这里输入内容... </textarea> <input type="submit" value="提交" /> </form> </body> </html>
# 引入 CGI 处理模块 import cgi, cgitb # 创建 FieldStorage的实例 form = cgi.FieldStorage() # 接收字段数据 if form.getvalue('textcontent'): text_content = form.getvalue('textcontent') else: text_content = "没有内容" print ("Content-type:text/html") print () print ("<html>") print ("<head>") print ("<meta charset="utf-8">") print ("<title>菜鸟教程 CGI 测试实例</title>") print ("</head>") print ("<body>") print ("<h2> 输入的内容是:%s</h2>" % text_content) print ("</body>") print ("</html>")
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form action="/cgi-bin/dropdown.py" method="post" target="_blank"> <select name="dropdown"> <option value="runoob" selected>菜鸟教程</option> <option value="google">Google</option> </select> <input type="submit" value="提交"/> </form> </body> </html>
# 引入 CGI 处理模块 import cgi, cgitb # 创建 FieldStorage的实例 form = cgi.FieldStorage() # 接收字段数据 if form.getvalue('dropdown'): dropdown_value = form.getvalue('dropdown') else: dropdown_value = "没有内容" print ("Content-type:text/html") print () print ("<html>") print ("<head>") print ("<meta charset="utf-8">") print ("<title>菜鸟教程 CGI 测试实例</title>") print ("</head>") print ("<body>") print ("<h2> 选中的选项是:%s</h2>" % dropdown_value) print ("</body>") print ("</html>")
print ('Content-Type: text/html') print ('Set-Cookie: name="菜鸟教程";expires=Wed, 28 Aug 2016 18:30:00 GMT') print () print (""" <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h1>Cookie set OK!</h1> </body> </html> """)
import os import http.cookies print ("Content-type: text/html") print () print (""" <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h1>读取cookie信息</h1> """) if 'HTTP_COOKIE' in os.environ: cookie_string=os.environ.get('HTTP_COOKIE') c= http.cookies.SimpleCookie() # c=Cookie.SimpleCookie() c.load(cookie_string) try: data=c['name'].value print ("cookie data: "+data+"<br>") except KeyError: print ("cookie 没有设置或者已过去<br>") print (""" </body> </html> """)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <form enctype="multipart/form-data" action="/cgi-bin/save_file.py" method="post"> <p>选中文件: <input type="file" name="filename" /></p> <p><input type="submit" value="上传" /></p> </form> </body> </html>
import cgi, os import cgitb; cgitb.enable() form = cgi.FieldStorage() # 获取文件名 fileitem = form['filename'] # 检测文件是否上传 if fileitem.filename: # 设置文件路径 fn = os.path.basename(fileitem.filename) open('/tmp/' + fn, 'wb').write(fileitem.file.read()) message = '文件 "' + fn + '" 上传成功' else: message = '文件没有上传' print (""" Content-Type: text/html <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p>%s</p> </body> </html> """ % (message,))
# HTTP 头部 print ("Content-Disposition: attachment; filename="foo.txt"") print () # 打开文件 fo = open("foo.txt", "rb") str = fo.read(); print (str) # 关闭文件 fo.close()