在 windows 上不容易操作,一般服务器都为unix, linux
首先是要在windows安装Apache
并配置一些环境
1:在官网下载:点击打开链接
点击进入:点击打开链接
点击进入:点击打开链接
点击进入:点击打开链接
下载好解压就行
2:配置
首先配置目录里的 httpd.conf 文件
此文件在 E:\httpd-2.4.23-x86-vc14\Apache24\conf 里
根据自己解压的目录寻找
找到后打开
更改定义的 SRVROOT
将其改为 E:\httpd-2.4.23-x86-vc14\Apache24
根据自己解压目录来改
然后在根据自己的情况改一些变量:
如果你想指定其他运行CGI脚本的目录,可以修改httpd.conf配置文件,如下所示:
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
在 AddHandler 中添加 .py 后缀,这样我们就可以访问 .py 结尾的 python 脚本文件:
AddHandler cgi-script .cgi .pl .py
其中 AddHandler 的值是注释了的,所以要改掉
这样就可以开启服务了
3:开启服务
在 E:\httpd-2.4.23-x86-vc14\Apache24\bin 命令行输入> httpd.exe -k install
提示安装成功,并提醒可以开启服务
开启服务成功就可以在浏览器实验了
此服务器默认监听 80 端口
如果 80 端口被占用可以在 你可以在 httpd.conf 里面改 Listen 80
实验:
直接输入 localhost
像这样就成功了
4:CGI 编程
使用python 写一个 text.py
展示一下访问服务器与客服端的交流
首先些 text.py:
#!E:\python\python.exe
print("Content-type:text/html")
print()
print("<html>")
print("<head>")
print("<meta charset = \"utf-8\" />")
print("<title>my first cgi program</title>")
print("</head>")
print("<body>")
print("<h2>hello world!!</h2>")
print("</body>")
print("</html>")
第一行安装目录一定要写对,不然会出现错误
[Tue Aug 30 17:38:41.990029 2016] [win32:error] [pid 6036:tid 880] [client 127.0.0.1:22865] AH02102: E:/httpd-2.4.23-x86-vc14/Apache24/cgi-bin/test.py is not executable; ensure interpreted scripts have "#!" or "'!" first line //此错误就是没有添加运行程序
[Tue Aug 30 17:38:41.990029 2016] [cgi:error] [pid 6036:tid 880] (9)Bad file descriptor: [client 127.0.0.1:22865] AH01222: don't know how to spawn child process: E:/httpd-2.4.23-x86-vc14/Apache24/cgi-bin/test.py
[Tue Aug 30 17:47:58.650868 2016] [cgi:error] [pid 6036:tid 880] (OS 2)系统找不到指定的文件。 : [client 127.0.0.1:22971] couldn't create child process: 720002: test.py //此错误就是运行程序安装目录写错造成的
[Tue Aug 30 17:47:58.650868 2016] [cgi:error] [pid 6036:tid 880] (OS 2)系统找不到指定的文件。 : [client 127.0.0.1:22971] AH01223: couldn't spawn child process: E:/httpd-2.4.23-x86-vc14/Apache24/cgi-bin/test.py
之后将 text.py 的属性更改为 755 (这是unix,linux上经常用到的权限)
在 windows 上就直接改,其实也可以不改就能运行
之后就可以直接在浏览器运行了
输入 http://localhost/cgi-bin/test.py 即可
更多python-CGI编程信息且看点击打开链接