python cgi ajax - 使用CGIHTTPServer实现一个ajax程序。
ajax技术得时候发现使用python的cgi模块,就不用非得搭建一个apache环境来作测试了!
Ajax python 测试笔记
首先咱们须要写一个html网页里面涉及到JavaScript和xml相关只是请参阅http://www.w3schools.com/
time.html 放在建立的目录下。
[code]
function ajaxFunction()
{
var xmlHttp;
try
{
//Firefox,Opera 8.0+ safari
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
//Inter Expplorer
try
{
xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
document.myForm.time.value = xmlHttp.responseText;
}
}
xmlHttp.open("Get","http://localhost:8000/cgi-bin/time.py",true);
xmlHttp.send(null);
}
Name:
Time:
[code]
接着咱们要安装一个python 这个能够去www.python.org 下载,安装以后要把python目录加到系统得path里,这个右键个人电脑,属性里修改。
任意建立一个目录名字随便起,在这个目录下再创建一个文件run.bat 和一个目录cgi-bin
run.bat 的内容就一行,双击以后启动服务
python -m CGIHTTPServer
cgi-bin目录下建立文件time.py 内容以下
[code]
#---------------------------------------------------------------------------
#!/usr/bin/env python
# -*- coding gb2312 -*-
# Simple CGI Example - Chapter 18 - simple.cgi
import cgitb
cgitb.enable()
import time
print "Content-type: text/html" #http 头信息必须
print #这个空格必定要输入
print time.strftime('%Y-%m-%d %X', time.localtime() )
#---------------------------------------------------------------------------
[code]
浏览器访问 http://localhost:8000/time.html
在输入框里任意输入,在time框里就会出现时间了。javascript