CGI编程

一、CGI编程



简单编程过程:


1.服务器的结构创建:

首先要一个web根目录(这里是web-app文件夹),所有的CGI或者py文件要存放在 cgi-bin目录下,html则不能存放在该目录下。



2.启动python的web服务器(适合简单的测试)
python -m CGIHTTPServe


3.编辑脚本
[root@www cgi-bin]# cat  test.cgi
#!/usr/bin/python
print "Content-type:text/plain"
print #打印空行来结束首部,没有空行则也无法解析

print "hello!"
[root@www cgi-bin]# 

#!/usr/bin/python   要加入pound bang行(必须是第一行)
print "Content-type:text/plain"表示页面是普通文件,如果页面是html文件,则应该为 print "Content-type:text/html"



[root@www cgi-bin]# cat  test1.cgi
#!/usr/bin/python                   #必须加这一行内容指定python,否则也无法执行脚本。
# -*- coding: utf-8 -*-
                   
# 导入cgi模块,以便后面调用相关方法,这个简单例子可以不用写
import cgi

# 向CGI脚本返回结果的时候,先返回一个 http头文件,不然得不到 http。这一步不能省略
header = 'Content-Type: text/html\n\n'      
                   
html = '<h3>hello world</h3>'
#打印返回的内容
print header         #因为前面已经在文本中换行,这里没换行也没错。
print html
[root@www cgi-bin]#

4.设置文件许可

chmod +755 filename

5.访问测试

http://IP:8000/cgi-bin/test1.cgi

效果:



注意:在cgi-bin目录下的文件不一定要命名为.cgi,用.py也可以。



6.使用cgitb调试

[root@www cgi-bin]# cat test3.py
#!/usr/bin/python
import cgitb
cgitb.enable()
print "Content-type: text/html"
print
print 1/0
print "hello!"
[root@www cgi-bin]#

调试结果:






7.cgi模块的使用

例1(getvalue获取值):

[root@www cgi-bin]# cat test4.py  
#!/usr/bin/python 
import cgi 
form = cgi.FieldStorage() 
name= form.getvalue('name','world') 

print 'Content-type: text/plain' 
print 

print 'hello,%s' % name 
[root@www cgi-bin]# 

例2(简单表单的处理):


form.html
<!doctype html>
< html >
     < head >
         < title >
             hello python cgi
         </ title >
     </ head >
     < body >
         < form  action = "/cgi-bin/form.py" >
             < label  for = "" >username:</ label >< input  type = "text"  name = "username"  value = "username" >
             < label  for = "" >password:</ label >< input  type = "password"  name = "password"  value  "" >
             < input  type = "submit" >
         </ form >
     </ body >
</ html >


form.py文件

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
import cgi 
header = 'Content-Type: text/html\n\n' 
html = '<h3>接受处理表单数据\n</h3>' 
#打印返回的内容 
print header 
print html 
# 接受表达提交的数据 
form = cgi.FieldStorage() 
print '接收表达get的数据 :',form 
print '<p />' 
# 解析处理提交的数据 
username = form['username'].value                   #也可以username = form.getvalue('username')用getvalue获取
password = form['password'].value 
formhtml = ''' 
<label for="">username:</label><input type="text" value="%s"> 
<label for="">password:</label><input type="text" value = "%s"> 
''' 
print formhtml % (username,password)


结果分析:



例3(修改name,并显示修改):

[root@www cgi-bin]# cat test4.py  
#!/usr/bin/python 
import cgi 
form = cgi.FieldStorage() 
name= form.getvalue('name','world') 

print 'Content-type: text/html' 
print 

#print 'hello,%s' % name 

print  """ 

<html> 
    <head> 
        <title>change name</title> 
    </head> 
    <body> 
        <h1>hello.%s!</h1> 
        <form action='test4.py'> 
        change name <input type='text' name='name' /> 
        <input type='submit'/> 
        </form> 
     </body> 
</html> 

""" 
% name 
[root@www cgi-bin]# 




HTTP头部

文件内容中的" Content-type:text/html\r\n\r\n"即为HTTP头部的一部分,它会发送给浏览器告诉浏览器文件的内容类型。

HTTP头部的格式如下:

HTTP 字段名: 字段内容

例如
Content-type: text/html\r\n\r\n

以下表格介绍了CGI程序中HTTP头部经常使用的信息:

描述
Content-type: 请求的与实体对应的MIME信息。例如: Content-type:text/html
Expires: Date 响应过期的日期和时间
Location: URL 用来重定向接收方到非请求URL的位置来完成请求或标识新的资源
Last-modified: Date 请求资源的最后修改时间
Content-length: N 请求的内容长度
Set-Cookie: String 设置Http Cookie





错误分析:

1.



解决:

cgi或py文件中要有 print "Content-type:text/plain"这句,不然无法解析




2.在Apache服务中尝试用cgi-bin

不能命名为.py,不然无法解析执行,只能为.cgi

在/var/www/html/cgi-bin下创建脚本

报错:

访问页面:
You don't have permission to access /cgi-bin/ on this server.

查看日志:
cat /var/log/httpd/error_log
报错:Attempt to invoke directory as script

修改httpd.conf配置文件:

replace this:

ScriptAlias /cgi-bin /var/www/html/cgi-bin/  

with  :

Alias /cgi-bin/ "/var/www/html/cgi-bin/"
#
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/var/www/html/cgi-bin/">
    AllowOverride None
    Options None FollowSymLinks +ExecCGI
    Order allow,deny
    Allow from all
</Directory>

AddHandler cgi-script .cgi .pl

DirectoryIndex index.html index.html.var index.php index.cgi    #这里不加index.cgi的话则默认的cgi-bin目录无法访问,但是下面的文件,指定的话还是可以访问的。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值