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 ...
随机推荐
- Openjudge-计算概论(A)-求一元二次方程的根
描述: 利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax2 + bx + c =0的根 ...
- 华硕笔记本怎么进入PE之前的BIOS设置
1.先要制作一个U盘的PE启动盘,建议使用WIN8 PE 2.将制作好的PE启动盘接上电脑,开机按F2键进入BIOS ,先将[Secure]菜单下[Secure Boot Control]选项设置为[ ...
- SCALA STEP BY STEP
http://www.artima.com/scalazine/articles/steps.html http://hongjiang.info/scala/
- List泛型集合常用方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...
- Silverlight程序中访问配置文件
以下代码为本人在一Silverlight程序中访问Web端配置文件的代码: private void GetLoadNeed() { // 项目名称读取配置文件 WebClient wcConfigX ...
- web开发路径问题
1. web开发路径问题总结: http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html 2. JSP/SERVLET 路径问题 ...
- Redis简介二
一.直接安装 1.Windows版本的Redis下载地址:https://github.com/dmajkic/redis/downloads ,选择一个你想要下载的版本下载即可~ ...
- 照着例子学习protobuf-python
以下是照着python操作protobuf进行的protobuf-python的学习笔记: 首先是protobuf的下载与安装: 1 由于google被墙,所以去github上面搜索了一下protob ...
- Android任务栈TaskStack
Task:有多个Activity按顺序组成的一个完整的业务逻辑. 任务栈(TaskStack):新增的Activity放入栈中,点击back栈顶Activity从栈中退出. android:nohis ...
- hdu_2899_Strange fuction(三分查找)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2899 题意:让你解方程 题解:对于只有一个凸或者没有凸的图像,可以直接上三分解决. #include& ...