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. Just do it!!!

    4月英语竞赛期中考试 5月初级程序员考试 6月英语四级考试 7月期末考试 加油吧年轻人!

  2. C# 调用C++ DLL 的类型转换

    //C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byt ...

  3. 重识 CSS

  4. 转载 Deep learning:二(linear regression练习)

    前言 本文是多元线性回归的练习,这里练习的是最简单的二元线性回归,参考斯坦福大学的教学网http://openclassroom.stanford.edu/MainFolder/DocumentPag ...

  5. 用http-server 创建node.js 静态服务器

    今天做一本书上的例子,结果代码不能正常运行,查询了一下,是语法过时了,书其实是新买的,出版不久. 过时代码如下 var connect=require('connect'); connect.crea ...

  6. 【转】解决Windows不能在本地计算机启动apache tomcat

    http://blog.163.com/ftskwsg@126/blog/static/5623853020094494117827/ 这个方法解决了我的问题. 在windows下以服务的方式启动时提 ...

  7. 通过CSS实现各种方向的三角形

    #triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid tr ...

  8. LeetCode OJ 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. Python使用os.listdir()函数来获得目录中的内容

    摘自:http://it.100xuexi.com/view/otdetail/20130423/057606dc-7ad1-47e4-8ea6-0cf75f514837.html   1.在Pyth ...

  10. ios控件 UIControl

    < UIControl> 1 处理用户事件的控件的基类,如UIButton,UISlider等 2 一般不直接实例化,而是使用他的子类 3 可以通过跟踪触摸事件来设置和获取控件状态,并且这 ...