python web.py

发布时间:2019-09-03 08:52:04编辑:auto阅读(1715)

    web.py

    1.安装:

    wget http://webpy.org/static/web.py-0.37.tar.gz  

    tar xvfz web.py-0.37.tar.gz  

    cd web.py-0.37

    sudo python setup.py install  

    2.需求:

    先说下需求,http://10.75.7.237:8080/?ipaddress=10.75.7.29&count=100 ,最终得到这个ip的日志的前100行

    3.讲解:

    #!/usr/bin/python

    import web

    render = web.template.render('templates/') 这是告诉你的web展现页面放在那个位置

    urls = (

        '/', 'index'

    )

    这是告诉你访问/路径时所调用的类是index

    class index:

        def reset_sigpipe(self):

            signal.signal(signal.SIGPIPE,signal.SIG_DFL)



        def GET(self):

            ip=web.input(ipaddress=None,count=None)  让url可以传输多个值名为ipaddress和count

            print ip['ipaddress']   

            print ip['count']

            a=ip['ipaddress']

            address_dict={'10.13.144.':'bx','10.73.26.':'tc','10.75.7.':'yf','10.67.15.':'yq'}

            aa=a.split(".")

            b=aa[0:3]

            d=aa[-1]

            e=".".join(b)

            c=e+"."

            address_1=address_dict[c]

            f=str(d)

            g=address_1+f

            time_day=time.strftime('%Y-%m-%d')

            #h_1=subprocess.Popen('grep -R %s /data1/saelog/%s/error/'%(g,time_day),shell=True,preexec_fn=self.reset_sigpipe(),stdout=subprocess.PIPE)

            #h=h_1.stdout.read()

            h = os.popen('grep -R %s /data1/saelog/%s/error/' % (g,time_day))

            print h

            head_list=[]

            sum=0

            dict={}

            head_lines=ip['count']

            print head_lines

            if head_lines:

                    for i in h:

                            j=i.split(":")[1:]

                            head_list.append(j)

                    for kk in head_list:

                            dict[sum]=head_list[sum]

                            sum+=1

                            if sum == int(head_lines):

                                    break;

                    dict_json=json.dumps(dict)


            else:

                    for i in h:

                            j=i.split(":")[1:]

                            head_list.append(j)

                    for kk in head_list:

                            dict[sum]=head_list[sum]

                            sum+=1

                    dict_json=json.dumps(dict)

            print dict_json

            return render.index(dict_json)       传给页面展示的值

            #return "Hello, world!"


    if __name__ == "__main__":

        app = web.application(urls, globals())

        app.run()



    templates目录下

    index.html


    $def with (name)

    $if name:

            $name    name为代码传过来的值


    4.启动web.py

    python test.py  后面不跟端口默认就是8080


    5.访问http://10.75.7.237:8080/?ipaddress=10.75.7.29&count=100


关键字