Python CGI编程和CGIHTTPServer
Python2.7 的CGIHTTPServer
可以作为一个简单的HTTP服务器,能够调用cgi脚本
1 在任意目录下创建一个特殊的目录 cgi-bin ,用于存放自己写的脚本(.py或.cgi)
2 在 cgi-bin 所在目录 执行 python -m CGIHTTPServer ,启动服务器
3 在浏览器输入 IP:/cgi-bin/xxx.py 即可调用cgi-bin目录下的xxx.py脚本了(Linux可能要更改权限755)
示例1
hello.py #!/usr/bin/python #for Linux Windows: #!C:/Python34/python.exe
# -*- coding: utf-8 -*- print "Content-type:text/html\r\n\r\n" #HTTP的header 必不可少 \r\n\r\n表示头部结束,后面的内容都为body
print '''
<html>
<head>
<title>Hello from Python CGI</title>
</head>
<body>
<h1>Hello! This is my first CGI program!!!</h1>
</body>
</html>
'''
Headers
| 头 | 描述 |
|---|---|
| Content-type: | 请求的与实体对应的MIME信息。例如: Content-type:text/html |
| Expires: Date | 响应过期的日期和时间 |
| Location: URL | 用来重定向接收方到非请求URL的位置来完成请求或标识新的资源 |
| Last-modified: Date | 请求资源的最后修改时间 |
| Content-length: N | 请求的内容长度 |
| Set-Cookie: String | 设置Http Cookie |
Apache 执行cgi脚本
修改配置文件 /path/to/conf/httpd.conf
1 开启加载模块
LoadModule cgid_module /usr/lib/apache2/modules/mod_cgid.so
2 配置脚本目录别名
ScriptAlias /cgi-bin/ /home/zoro/cgi-bin/ #注意最后一个斜杠前后要保持一致 别名和目录名都可以随意起
3 设置脚本目录访问权限
<Directory "/home/zoro/cgi-bin">
Options ExecCGI //
Require all granted
</Directory>
另一个例子:
/home/sqd/cgi-bin/hello.sh
#/bin/sh echo "Content-Type: text/html; charset=utf-8" #header echo #表示header结束 不能少 echo "<h1>Hello from sh CGI.</h1>"
chmod 755 hello.sh
缺少header会报错: Internal Server Error
查看错误日志(/usr/local/apache2/logs/error_log)可见:malformed header from script 'hello.sh': Bad header: Hello from sh CGI.
Python CGI编程和CGIHTTPServer的更多相关文章
- Python CGI编程(转自易百)
Python CGI编程 Python的CGI编程,公共网关接口或CGI,Web服务器和一个自定义的脚本之间交换信息是一组定义的标准. 什么是CGI ? 公共网关接口或CGI,Web服务器和一 ...
- python CGI编程-----简单的本地使用(1)
本章节需要安装python开发工具,window平台安装地址:https://www.python.org/downloads/windows/,linux安装地址:https://www.pytho ...
- 转:python cgi编程
转:http://www.runoob.com/Python/python-cgi.html 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway ...
- 吴裕雄--天生自然python学习笔记:Python CGI编程
什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户 ...
- python CGI 编程实践
文章更新于:2020-03-05 注1:安装 python 参见: python 的安装使用和基本语法 注2:配置 web 环境参见: Windows&linux使用集成环境搭建 web 服务 ...
- python CGI编程Apache配置
1. 编辑http.conf,添加两行,路径可以自定义 <Directory "C:/AppServ/www/cgi-bin"> AllowOverride None ...
- Python CGI编程
CGI(Common Gateway Interface)通用网关接口,它是一段程序,运行在服务器上.如:HTTP服务器,提供同客户端HTML页面的接口. CGI程序可以是python脚本,PERL脚 ...
- Common Gateway Interface Python CGI编程
https://en.wikipedia.org/wiki/Gateway_(telecommunications) In telecommunications, the term gateway r ...
- 彻底解决python cgi 编程出现的编码问题
Answering this for late-comers because I don't think that the posted answers get to the root of the ...
随机推荐
- 给当前的URL添加/更新新的参数
/** * 给当前的URL添加/更新新的参数 * @param a 参数 * @param b 值 * @returns {string} 新的参数 * @constructor */ functio ...
- E - 小晴天老师系列——我有一个数列!
E - 小晴天老师系列——我有一个数列! Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/O ...
- POJ1113 Wall 凸包
题目大意:建立围墙将城堡围起来,要求围墙至少距离城堡L,拐角处用圆弧取代,求围墙的长度. 题目思路:围墙长度=凸包周长+(2*PI*L),另外不知道为什么C++poj会RE,G++就没问题. #inc ...
- 移动UI
UI设计需要关注:色彩,信息布局,交互流程,用户体验,尺寸等
- Android PagerAdapter的用法
转http://blog.csdn.net/look85/article/details/8563906 在写这个之前,真心需要吐槽一下…关于Android开发中,PageAdapter的用法在网上能 ...
- mysql出现Can't connect to MySQL server on 'localhost' (10061)的解决方法
网上搜索的一: 今天把mysql数据库拷贝到另外一台机上,结果连不上,报“Can't connect to MySQL server on 'localhost' (10061)“错误 到网上sear ...
- ContentPlaceHolderID属性
用来对应包含与当前内容关联的 ContentPlaceHolder 的 ID啊 说白了就是去找母版页相应的ContentPlaceHolder ,然后把内容扔进那里面去 <asp:Content ...
- Codeforces 237C
题目: Description You've decided to carry out a survey in the theory of prime numbers. Let us remind y ...
- Lowest Bit
Lowest Bit Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Su ...
- 传统 Ajax 已死,Fetch 永生
原谅我做一次标题党,Ajax 不会死,传统 Ajax 指的是 XMLHttpRequest(XHR),未来现在已被 Fetch 替代. 最近把阿里一个千万级 PV 的数据产品全部由 jQuery 的 ...
