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的更多相关文章

  1. Python CGI编程(转自易百)

    Python CGI编程 Python的CGI编程,公共网关接口或CGI,Web服务器和一个自定义的脚本之间交换信息是一组定义的标准.     什么是CGI ? 公共网关接口或CGI,Web服务器和一 ...

  2. python CGI编程-----简单的本地使用(1)

    本章节需要安装python开发工具,window平台安装地址:https://www.python.org/downloads/windows/,linux安装地址:https://www.pytho ...

  3. 转:python cgi编程

    转:http://www.runoob.com/Python/python-cgi.html 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway ...

  4. 吴裕雄--天生自然python学习笔记:Python CGI编程

    什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户 ...

  5. python CGI 编程实践

    文章更新于:2020-03-05 注1:安装 python 参见: python 的安装使用和基本语法 注2:配置 web 环境参见: Windows&linux使用集成环境搭建 web 服务 ...

  6. python CGI编程Apache配置

    1. 编辑http.conf,添加两行,路径可以自定义 <Directory "C:/AppServ/www/cgi-bin"> AllowOverride None ...

  7. Python CGI编程

    CGI(Common Gateway Interface)通用网关接口,它是一段程序,运行在服务器上.如:HTTP服务器,提供同客户端HTML页面的接口. CGI程序可以是python脚本,PERL脚 ...

  8. Common Gateway Interface Python CGI编程

    https://en.wikipedia.org/wiki/Gateway_(telecommunications) In telecommunications, the term gateway r ...

  9. 彻底解决python cgi 编程出现的编码问题

    Answering this for late-comers because I don't think that the posted answers get to the root of the ...

随机推荐

  1. C# html转mht

    项目中引入cdosys.dll组件,位置:  C:\Windows\System32\cdosys.dll CDO.ADODB 这些类都来自于cdosys.dll组件 /// <summary& ...

  2. MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)

    一.文件夹的创建 void CFileOperationDlg::OnButtonMakeFolder() { // TODO: Add your control notification handl ...

  3. phabricator 搭建

    os:debian7 Installation Guide :https://secure.phabricator.com/book/phabricator/ $ cd /data # 安装目录 da ...

  4. elipse图标注解

    Java中的访问修饰符(访问控制符)包括:public,protected,default,private.分别代表了不同的访问权限.如果省略,则被视为使用了默认的default作为访问修饰符.从字面 ...

  5. error while loading shared libraries: libseaudit.so.4: cannot open shared object file: Error 40

    安装共享库后要注意共享库路径设置问题, 如下: 1) 如果共享库文件安装到了/lib或/usr/lib目录下, 那么需执行一下ldconfig命令 ldconfig命令的用途, 主要是在默认搜寻目录( ...

  6. ACE_Event_Handler:事件响应入口

    1:ACE_Event_Handler类 头文件“Event_Handler.h” 在ACE Reactor框架中,ACE_Event_Handler是所有事件处理器的基类.ACE_Event_Han ...

  7. LR错误整理

    1.LoadRunner超时错误: 在录制Web服务器端,如果超过120秒服务器协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同. 错误现象1:Action.c(16): Er ...

  8. html5学习(二)音频audio

    音频格式 当前,audio 元素支持三种音频格式:   IE 9 Firefox 3.5 Opera 10.5 Chrome 3.0 Safari 3.0 Ogg Vorbis   √ √ √   M ...

  9. jsp的九大天王

    JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1. ...

  10. javascript中的throttle和debounce

    throttle 我们这里说的throttle就是函数节流的意思.再说的通俗一点就是函数调用的频度控制器,是连续执行时间间隔控制.主要应用的场景比如: 1.鼠标移动,mousemove 事件2.DOM ...