当前位置:   article > 正文

python web 入门碎碎念bottle(一)_python 图片 bottle

python 图片 bottle

人生苦短
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
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/375142
推荐阅读
相关标签
  

闽ICP备14008679号