python web 入门碎碎念bottle(一)

博客围绕bottle展开,介绍了调用静态资源(图片、css等)的方法,提及html中变量的绝对引用及注意事项,还说明了本机与外部访问的设置。最后给出源码情况,并列出后续计划,包括集成化函数、尝试面向对象和保持更新频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

人生苦短
bottle
一、调用静态资源(图片、css、js、字体。。。)
@route(‘/css/’)
def server_static(filepath):
return static_file(filepath, root=’./css’)
@route(‘/images/’)
def server_static(filepath):
return static_file(filepath, root=’./images’)
@route(‘/vendor/’)
def server_static(filepath):
return static_file(filepath, root=’./vendor’)
@route(‘/js/’)
def server_static(filepath):
return static_file(filepath, root=’./js’)
@route(‘/fonts/’)
def server_static(filepath):
return static_file(filepath, root=’./fonts’)

二、html中变量的绝对引用
默认绝对引用:{{name}}
非:{{!name}}
(注意: 调用input变量可能会有安全隐患)

三、本机访问与外部访问
本机:run(host=”localhost”, port=8080, debug=True)
外部:run(host=”0.0.0.0”, port=8080, debug=True)

源码(稚嫩 不优雅的):

from bottle import get, post, request, run,template,route,static_file
import datetime
import json
import math
from urllib import request

@route('/css/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='./css')
@route('/images/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='./images')
@route('/vendor/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='./vendor')
@route('/js/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='./js')
@route('/fonts/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='./fonts')

@route('/')
def callback():
    with request.urlopen('http://42.202.130.101:8080/cnszz-1.0-SNAPSHOT/selectSqlAction.action') as f:
        data = f.read()
        print('Status:', f.status, f.reason)
        for k, v in f.getheaders():
            print('%s: %s' % (k, v))
        print('Data:', data.decode('utf-8'))

    ddata = json.loads(data)
    fdata=ddata['datalist'][2999]['mname']
    print ("初始值2999:"+fdata)
    fdatatime=ddata['datalist'][2999]['time']
    i=2999
    time1=datetime.datetime.strptime("0:0:0", "%H:%M:%S")
    time2=datetime.datetime.strptime("0:0:0", "%H:%M:%S")
    output="<table class=\"table table-borderless table-striped table-earning\"><thead><tr><th>图号</th><th>开始时间</th><th>结束时间</th><th class=\"text-right\">状态</th><th class=\"text-right\">持续时间</th></tr></thead><tbody></tbody>"
    while i>0:
        fdatanow=ddata['datalist'][i]['mname']

        if fdatanow!=fdata:



            if fdata == "402C3-34-4-4":
                fdata2 = "402C3-34-4-4"
            elif fdata == "B6-40-C079-5701301-4/4":
                fdata2 = "402C3-34-4-2"
            elif fdata == "-":
                fdata2 = "设备空闲"
            elif fdata == "B6-40-C079-5401041-4/4":
                fdata2 = "B6-40-C079-5401041-4/4"

            elif fdata == "B6-40-C079-5701301-3/4 ":
                fdata2 = "B6-40-C079-5701301-3/4"

            fdatatimenow=ddata['datalist'][i]['time']
            status=ddata['datalist'][i+1]['status']
            t1=datetime.datetime.strptime(fdatatimenow, "%Y-%m-%d %H:%M:%S")
            t2=datetime.datetime.strptime(fdatatime, "%Y-%m-%d %H:%M:%S")
            time=t2-t1
            if status=="模具在线":
                time1+=time
                if int(math.modf(time.total_seconds()/3600)[1])==0:
                    put = str(
                        "<tr ><th>" + fdata2 + "</th><th>" + fdatatimenow + "</th><th>" + fdatatime + "</th><th color=\"green\">" + status + "</th><th>"  + str(
                            int(math.modf(time.total_seconds() / 3600)[0] * 60)) + "分钟</th></tr>")
                else:
                    put = str(
                        "<tr ><th>" + fdata2 + "</th><th>" + fdatatimenow + "</th><th>" + fdatatime + "</th><th color=\"green\">" + status + "</th><th>" + str(
                            int(math.modf(time.total_seconds() / 3600)[1])) + "小时" + str(
                            int(math.modf(time.total_seconds() / 3600)[0] * 60)) + "分钟</th></tr>")
            else:
                time2+=time
                if int(math.modf(time.total_seconds()/3600)[1])==0:
                    put = str(
                        "<tr ><th>" + fdata2 + "</th><th>" + fdatatimenow + "</th><th>" + fdatatime + "</th><th color=\"yellow\">" + status + "</th><th>"  + str(
                            int(math.modf(time.total_seconds() / 3600)[0] * 60)) + "分钟</th></tr>")
                else:
                    put = str(
                        "<tr ><th>" + fdata2 + "</th><th>" + fdatatimenow + "</th><th>" + fdatatime + "</th><th color=\"yellow\">" + status + "</th><th>" + str(
                            int(math.modf(time.total_seconds() / 3600)[1])) + "小时" + str(
                            int(math.modf(time.total_seconds() / 3600)[0] * 60)) + "分钟</th></tr>")

            output = output + put
            fdatatime=fdatatimenow
            fdata = fdatanow
        i-=1
    output=output+ "</table>"
    print(output)
    name="实时设备看板"
    return template('index',tname=output,name=name)


@route('/web2')
def callback():
    with request.urlopen('http://42.202.130.101:8080/cnszz-1.0-SNAPSHOT/selectSqlAction.action') as f:
        data = f.read()
        print('Status:', f.status, f.reason)
        for k, v in f.getheaders():
            print('%s: %s' % (k, v))
        print('Data:', data.decode('utf-8'))

    ddata = json.loads(data)
    fdata=ddata['datalist'][2999]['mname']
    print ("初始值2999:"+fdata)
    fdatatime=ddata['datalist'][2999]['time']
    i=2999
    time1=datetime.datetime.strptime("0:0:0", "%H:%M:%S")
    time2=datetime.datetime.strptime("0:0:0", "%H:%M:%S")
    output="<table class=\"table table-borderless table-striped table-earning\"><thead><tr><th>图号</th><th>开始时间</th><th>结束时间</th><th class=\"text-right\">状态</th><th class=\"text-right\">持续时间</th></tr></thead><tbody></tbody>"
    while i>0:
        fdatanow=ddata['datalist'][i]['mname']

        if fdatanow!=fdata:



            if fdata == "402C3-34-4-4":
                fdata2 = "402C3-34-4-4"
            elif fdata == "B6-40-C079-5701301-4/4":
                fdata2 = "402C3-34-4-2"
            elif fdata == "-":
                fdata2 = "设备空闲"
            elif fdata == "B6-40-C079-5401041-4/4":
                fdata2 = "B6-40-C079-5401041-4/4"

            elif fdata == "B6-40-C079-5701301-3/4 ":
                fdata2 = "B6-40-C079-5701301-3/4"

            fdatatimenow=ddata['datalist'][i]['time']
            status=ddata['datalist'][i+1]['status']
            t1=datetime.datetime.strptime(fdatatimenow, "%Y-%m-%d %H:%M:%S")
            t2=datetime.datetime.strptime(fdatatime, "%Y-%m-%d %H:%M:%S")
            time=t2-t1
            if status=="模具在线":
                time1+=time
                if int(math.modf(time.total_seconds()/3600)[1])==0:
                    put = str(
                        "<tr ><th>" + fdata2 + "</th><th>" + fdatatimenow + "</th><th>" + fdatatime + "</th><th >" + status + "</th><th>"  + str(
                            int(math.modf(time.total_seconds() / 3600)[0] * 60)) + "分钟</th></tr>")
                else:
                    put = str(
                        "<tr ><th>" + fdata2 + "</th><th>" + fdatatimenow + "</th><th>" + fdatatime + "</th><th>" + status + "</th><th>" + str(
                            int(math.modf(time.total_seconds() / 3600)[1])) + "小时" + str(
                            int(math.modf(time.total_seconds() / 3600)[0] * 60)) + "分钟</th></tr>")
            else:
                time2+=time
                put= ""
            output=output+put
            fdatatime=fdatatimenow
            fdata = fdatanow
        i-=1

    output=output+ "</table>"
    name="实时数据看板(去空)"
    return template('index',tname=output,name=name)


@route('/web3')
def callback():
    with request.urlopen('http://42.202.130.101:8080/cnszz-1.0-SNAPSHOT/selectSqlAction.action') as f:
        data = f.read()
        print('Status:', f.status, f.reason)
        for k, v in f.getheaders():
            print('%s: %s' % (k, v))
        print('Data:', data.decode('utf-8'))
    tuhaoarr=[]
    timearr=[]
    cishuarr=[]
    ddata = json.loads(data)
    fdata=ddata['datalist'][2999]['mname']
    print ("初始值2999:"+fdata)
    fdatatime=ddata['datalist'][2999]['time']
    i=2999
    time1=datetime.datetime.strptime("0:0:0", "%H:%M:%S")
    time2=datetime.datetime.strptime("0:0:0", "%H:%M:%S")
    output="<table class=\"table table-borderless table-striped table-earning\"><thead><tr><th>图号</th><th>上设备次数</th><th>累计调试时间</th></tr></thead><tbody></tbody>"
    while i>0:
        fdatanow=ddata['datalist'][i]['mname']

        if fdatanow!=fdata:
            #纠正错误数据
            if fdata == "402C3-34-4-4":
                fdata2 = "402C3-34-4-4"
            elif fdata == "B6-40-C079-5701301-4/4":
                fdata2 = "402C3-34-4-2"
            elif fdata == "-":
                fdata2 = "设备空闲"
            elif fdata == "B6-40-C079-5401041-4/4":
                fdata2 = "B6-40-C079-5401041-4/4"
            elif fdata == "B6-40-C079-5701301-3/4 ":
                fdata2 = "B6-40-C079-5701301-3/4"

            result = fdata2 in tuhaoarr
            if result:

                tuhao1 = tuhaoarr.index(fdata2)
                cishuarr[tuhao1]+=1
                fdatatimenow = ddata['datalist'][i]['time']

                t1 = datetime.datetime.strptime(fdatatimenow, "%Y-%m-%d %H:%M:%S")
                t2 = datetime.datetime.strptime(fdatatime, "%Y-%m-%d %H:%M:%S")
                time = t2 - t1
                timearr[tuhao1]+=time

            else:

                tuhaoarr.append(fdata2)
                cishuarr.append(1)
                fdatatimenow = ddata['datalist'][i]['time']
                t1 = datetime.datetime.strptime(fdatatimenow, "%Y-%m-%d %H:%M:%S")
                t2 = datetime.datetime.strptime(fdatatime, "%Y-%m-%d %H:%M:%S")
                time = t2 - t1
                timearr.append(time)
            fdatatime=fdatatimenow
            fdata = fdatanow
        i-=1


    j=0
    while j<len(tuhaoarr):
        if tuhaoarr[j]=="设备空闲":
            print("空空不输出")
        else:
            put = str(
            "<tr ><th>" + tuhaoarr[j] + "</th><th>" + str(cishuarr[j]) + "</th><th>" +str(int(math.modf(timearr[j].total_seconds()/3600)[1])) + "小时"+str(int(math.modf(timearr[j].total_seconds()/3600)[0]*60))+"分钟</th></tr>")
            output=output+put
        j+=1
    output = output + "</table>"
    name="图号统计看板"
    return template('index',tname=output,name=name)

run(host="0.0.0.0", port=8080, debug=True)

下一步:
1、集成化函数
2、尝试面向对象
3、争取每周三篇更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值