免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4416 | 回复: 11
打印 上一主题 下一主题

perl 制表web表格出错,搞不定了。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-24 14:08 |只看该作者 |倒序浏览
20可用积分
现在在shell下写了一个automation testing script, 会产生一个log文件,格式如下
2008-12-20:18:20   dtx100  sles10.2-64bit  collectinv  successful
***
***
    日期                    计算机名     系统平台           运行命令    运行结果     共五列

怎么样用perl来写cgi, 把这个log文件做成web形式我是cgi新手,以前没写过,现在PM让尽快搞定,所以想请大虾们帮忙。悬赏20分。想手下留情,千万别拍砖

-----------------------------------------------------------------------------------------------
|                          |                   |                  |                     |                            |      
|        DATE            |    OS TYPE  |  IP             |    COMMAND|     RESULTS          |
|________ _______| ____________________________________________ ___________|
|                          |                   |                  |                     |                            |
|                          |                   |                  |                     |                            |
|                          |                   |                  |                     |                            |
| ______________ |_________________________________________________________  |
|                          |                   |                  |                     |                            |
|                          |                   |                  |                     |                            |
|                          |                   |                  |                     |                            |
-----------------------------------------------------------------------------------------------

按cobrawgl 的做法写了一个脚本perl4.pl

# vi perl4.pl

#!/usr/bin/perl

#
# log.pl
#

use strict;
use warnings;

use HTML::Template;

my $template = HTML::Template->new(filename => 'log.tmpl');

my @log;
while (<DATA>) {
        my %t;
        @t{qw/date computer platform command result/} = (split);
        push @log, \%t
}
$template->param(log => \@log);

print $template->output;

__DATA__
2008-12-20:18:20   dtx100  sles10.2-64bit  collectinv  successful
2008-12-20:18:20   dtx100  sles10.2-64bit  collectinv  successful
2008-12-20:18:20   dtx100  sles10.2-64bit  collectinv  successful
2008-12-20:18:20   dtx100  sles10.2-64bit  collectinv  successful
2008-12-20:18:20   dtx100  sles10.2-64bit  collectinv  successful
2008-12-20:18:20   dtx100  sles10.2-64bit  collectinv  successful
2008-12-20:18:20   dtx100  sles10.2-64bit  collectinv  successful

# vi  log.tmpl

#
#log.tmpl
#

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE> Log </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
</HEAD>

<BODY>
  <table>
  <tr><th>..</th><th>....</th><th>....</th><th>....</th><th>....< /th><tr>
  <tmpl_loop name="log">
        <tr>
                <td><tmpl_var name=date></td><td><tmpl_var name=computer></td><td><tmpl_var name=platform></td><td><tmpl_var name=command></td><td><tmpl_var name=result></td>
        </tr>
  </tmpl_loop>
  </table>
</BODY>
</HTML>


3. run ./perl4.sh

#./perl4.sh

#
#log.tmpl
#

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE> Log </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
</HEAD>

<BODY>
  <table>
  <tr><th>..</th><th>....</th><th>....</th><th>....</th><th>....< /th><tr>

        <tr>
                <td>2008-12-20:18:20</td><td>dtx100</td><td>sles10.2-64bit</td><td>collectinv</td><td>successful</td>
        </tr>

        <tr>
                <td>2008-12-20:18:20</td><td>dtx100</td><td>sles10.2-64bit</td><td>collectinv</td><td>successful</td>
        </tr>

        <tr>
                <td>2008-12-20:18:20</td><td>dtx100</td><td>sles10.2-64bit</td><td>collectinv</td><td>successful</td>
        </tr>
******
******
没报错,但在web里打开perl4.pl是空的,。不知道怎么回事。

最佳答案

查看完整内容

哦,你要直接用的话,还得 加上 CGI 模块来打印个 headeruse CGI qw/:standard/;use HTML::Template;...print header, $template->output;还有,把那个 log.tmpl 也改改,在 后面加上 好显示中文 改称 更好看些 [ 本帖最后由 cobrawgl 于 2008-12-24 14:13 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-12-24 14:08 |只看该作者
哦,你要直接用的话,还得 加上 CGI 模块来打印个 header

use CGI qw/:standard/;
use HTML::Template;

...

print header, $template->output;

还有,把那个 log.tmpl 也改改,在 <head> 后面加上
<meta http-equiv="Content-Type" content="text/html; charset=gbk">

好显示中文
<table> 改称 <table border="1"> 更好看些



[ 本帖最后由 cobrawgl 于 2008-12-24 14:13 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-12-24 14:30 |只看该作者
原帖由 cobrawgl 于 2008-12-24 14:12 发表
哦,你要直接用的话,还得 加上 CGI 模块来打印个 header

use CGI qw/:standard/;
use HTML::Template;

...

print header, $template->output;

还有,把那个 log.tmpl 也改改,在  后面加上


...


谢谢,还是有点小问题


1. 这个data 是放在perl4.pl里,我的shell脚本自动生成是放在一个目录下,怎么样让perl4.sh去读那个特定目录下的log文件。

2. 怎么定义这个表的表宽,及数据居中。(这个问题不急)

[ 本帖最后由 huanghaojie 于 2008-12-24 14:35 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-12-24 14:31 |只看该作者
cobrawgl   帅哥
大天使

积分已悬赏,还得请你帮忙解决一下

论坛徽章:
0
5 [报告]
发表于 2008-12-24 14:38 |只看该作者
收人钱财,替人消灾
重新给你弄个,你设置一下变量就可以了

--------------------------------------------------------------------
#!/usr/bin/perl

use strict;
use warnings;

use CGI qw/:standard/;
use HTML::Template;

use IO::File;

my $tmpl_file = '/your/file/path';  # Change it
my $data_file = '/your/file/path';  # Change it

my $template = HTML::Template->new(filename => $tmpl_file);
my $file = IO::File->new($data_file);

my @log;
while ($_ = $file->getline) {
        my %t;
        @t{qw/date computer platform command result/} = (split);       
        push @log, \%t
}
$template->param(log => \@log);

print header, $template->output;


--------------------------------------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <meta http-equiv="Content-Type" content="text/html; charset=gbk">
  <TITLE> Log </TITLE>
</HEAD>

<BODY>
  <table border="1">
  <tr><th>日期</th><th>计算机名</th><th>系统平台</th><th>运行命令</th><th>运行结果</th><tr>
  <tmpl_loop name="log">
        <tr>
                <td><tmpl_var name=date></td><td><tmpl_var name=computer></td><td><tmpl_var name=platform></td><td><tmpl_var name=command></td><td><tmpl_var name=result></td>
        </tr>
  </tmpl_loop>
  </table>
</BODY>
</HTML>

论坛徽章:
0
6 [报告]
发表于 2008-12-24 14:56 |只看该作者
原帖由 cobrawgl 于 2008-12-24 14:38 发表
收人钱财,替人消灾
重新给你弄个,你设置一下变量就可以了

--------------------------------------------------------------------
#!/usr/bin/perl

use strict;
use warnings;

use C ...


cobrawgl   非常感谢了, 看来这年头还是手头上有点积分好呀,现在开始拼命的挣积分,关健时候才能派上用场。

论坛徽章:
0
7 [报告]
发表于 2008-12-24 14:59 |只看该作者
下面要开始按一个python的来改这个perl了,
#!/usr/bin/python

import cPickle as p
import sys
import os
import cgi

PLATFORMNUM=4
DATENUM=30
MYIP='9.125.53.162'
#INDEX_PATH='/new_cim_harness/logindex/'
INDEX_PATH='/var/www/cgi-bin/log/'
#LOG_PATH='/new_cim_harness/logcontent/'
LOG_PATH='/var/www/cgi-bin/log/'
form=cgi.FieldStorage()
print "Content-type: text/html\r\n\r\n"

#------------------------------------------------#
#    This function read a string to array,
#    each word is an item in the array.
#    Use blank as the separator.
#------------------------------------------------#
def readtoarray(a,s):
     i=1
     j=0
     l=len(s)
     while i<l:
         if s==' ' or i==l-1:
             a.append(s[j:i])
             j=i+1
         i=i+1

#---------------------------------------------------------------------------#
#    This function load data file to python dictionary dict.
#    Key of dict is data.
#    Each record of dict is a matrix of the following content:
#    Test_Bed PST Agent_Platform Agent_IP
#    Success_Num Fail_Num Disabled_Num Result_File
#
#    Before calling this function, PLATFORMNUM and DATENUM must be set.
#--------------------------------------------------------------------------#
def loadfile(dict, datelist, fname, pro='tmp.log'):
     #cmd='cp -f ' + fname + ' /tmp/tmp2.log'
     #os.system( cmd )
     #cmd='sort /tmp/tmp2.log  -t" " -k 1 -r|head -' + str(PLATFORMNUM*DATENUM) + ' >/tmp/tmp.log'
     cmd='sort ' + fname + ' -t" " -k 1 -r|head -' + str(PLATFORMNUM*DATENUM) + ' >/tmp/' + pro
     os.system( cmd )
     #os.system( 'id' )
     #dict={}
    #os.system('cat /tmp/tmp.log')
     f=file('/tmp/'+pro)

论坛徽章:
0
8 [报告]
发表于 2008-12-24 14:59 |只看该作者
f=file('/tmp/'+pro)
     while True:
         line = f.readline()
         if len(line)==0:
             break;
         a=[]
         readtoarray(a, line)
         key=a[0]
         del a[0]
         if key in dict:
             dict[key].append(a)
         else:
             dict[key]=[a]
     f.close()
     #datelist=[]
     for d in dict:
         datelist.append(d)
     datelist.sort()


#os.system('echo "passw0rd"|gsa_login -p liuycdl')

print "<html>"
print '''<script>
     function dosubmit( dateobj ) {
         dateobj.form.submit();
     }
     </script>'''

#for product, combo_name in DICTFILES.items():
def showframe():
     dict={}
     datelist=[]
     product = 'SVC4.3'
     if form.has_key("product"):
         product = form["product"].value
     if form.has_key("type"):
         type = form["type"].value
     #pfile='/gsa/bvrgsa/home/m/a/mayao/testindex/' + product + '.intrinsic.index'
     pfile=INDEX_PATH + product + '.index'
     loadfile(dict, datelist, pfile, product)
     #print datelist[0]
     l=len(datelist)
     selected=datelist[l-1]
     if form.has_key("rundate"):
         selected=form["rundate"].value
     print '''
     <hr noshade>
     <h1>''' + product + '  ' + selected + '''</h1>
     <form method=post action="/cgi-bin/nightly_frame.cgi">
     <table width="100%">
     <tr>
         <th colspan="3" align="center">
         <h3>
             <u>''' + type + '''</u>
             <u>''' + type + '''</u>
         </h3>
         </th>
         <th colspan="3" align="center">
             <input type=hidden name=product value="''' + product + '''">
             <select name="rundate" onChange="dosubmit(this);">
     '''
     for i in range (1, l+1):
         if datelist[l-i]==selected:
             print '<option value="' + datelist[l-i] + '" selected>',datelist[l-i],'</option>'
         else:
             print '<option value="' + datelist[l-i] + '">',datelist[l-i],'</option>'
     print '''
             </select>
         </th>
     </tr>
     </table>
     </form>
     <table width="100%">
     </table>
     <table width="90%" border=2>
     <tr>
     <th width="10%" align="center">Test Date</th>
     <th width="18%" align="center">System Notes</th>
     <th width="15%" align="center">System IP</th>
     <th width="10%" align="center">System Notes</th>
     <th width="13%" align="center">CLI Command</th>
     <th width="10%" align="center">Test Results</th>
     <th width="20%" align="center">Detail Report</th>
     </tr>
     '''
     for item in dict[selected]:
         print '''
             <tr>
             <th width="18%" align="center">''' + item[0] + '''</th>
             <th width="15%" align="center">''' + item[1] + '''</th>
             <th width="10%" align="center">''' + item[2] + '''</th>
             <th width="13%" align="center">''' + item[3] + '''</th>
             <th width="10%" align="center">''' + item[4] + '''</th>
             <th width="10%" align="center">''' + item[5] + '''</th>
             <th width="20%" align="center"><a href=https://bvrgsa.ibm.com/projects/i''' + item[6] + ''' target="_new">Click here</a></th>
             </tr>'''
     print '''</table>
     '''

showframe()

print "</html>"

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
9 [报告]
发表于 2008-12-24 14:59 |只看该作者
原帖由 huanghaojie 于 2008-12-24 14:56 发表


cobrawgl   非常感谢了, 看来这年头还是手头上有点积分好呀,现在开始拼命的挣积分,关健时候才能派上用场。

等你赚到一定积分的时候就该挣别人的积分了

论坛徽章:
0
10 [报告]
发表于 2008-12-24 15:08 |只看该作者
python 也有那个模板的

我看python怎么头会疼呢

[ 本帖最后由 cobrawgl 于 2008-12-24 15:09 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP