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 ...
随机推荐
- Python in minute
Python 性能优化相关专题: https://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/ Python wikipedi ...
- Silverlight程序中访问配置文件
以下代码为本人在一Silverlight程序中访问Web端配置文件的代码: private void GetLoadNeed() { // 项目名称读取配置文件 WebClient wcConfigX ...
- html各元素中的区别
HTML中DIV与SPAN的区别 html的div和span, 经常会用到, 尤其是前者. 1. div是块级元素, 实际上就是一个区域, 主要用于容纳其他标签. 默认的display属性是block ...
- jsp标签简介
一.jsp标签简介 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. jsp的常用标签有 ...
- UVA 10480 Sabotage
最小割+输出方案 #include<cstdio> #include<cstring> #include<string> #include<cmath> ...
- iOS不可变数组的所有操作
#pragma mark 创建数组 //1.通过对象方法创建数组 NSArray * array = [[NSArray alloc]initWithObjects:@"One", ...
- Linux下编译C代码,出现tan函数报错的情况
undefined reference to `tan' 但是已经包含了头文件 <math.h>了,可还是报错,说是找不到tan 这个问题的原因不是很清楚, 但是网上给出的方案,就是编译的 ...
- VBS 文件选择框,选择Excel文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 on error resume Next Set objDialog=CreateObject("UserAcc ...
- git基本命令--status, add, diff, commit, log
git status: git status命令的输出十分详细,但其用语有些繁琐. 如果你使用 git status -s 命令或 git status --short 命令,你将得到一种更为紧凑的格 ...
- windows7安装oracle 10g
1.出现如下错误 解决办法: ①确保你有该文件夹的完全控制权.文件夹点右键->属性->安全->高级->所有者->改为自己->编辑自己的权限为完全控制. ②将setu ...
