bottle微框架從註冊到應用(一) 基礎配置

2021-09-26 23:22:17 字數 3257 閱讀 5839

現在很多寫bottle微框架的博主都是糊弄事,簡單寫乙個路由入口就不了了事,而談不到bottle如何解決具體專案,因此想通過一次小的練習來讓大家進一步了解bottle微框架的魅力,並可以通過自己的努力寫出乙個小的專案。

這裡將使用官方推薦實用的gevent模組,突破執行緒池的限制,建議直接複製。

#!/usr/bin/python

# -*- coding: utf-8 -*-

from gevent import monkey; monkey.patch_all()

from bottle import route, static_file, run, template_path

import logging

import os

import sys

base_dir = os.path.dirname(os.path.abspath(__file__))

@route('')

def server_static(filename):

"""定義static下所有的靜態資源路徑"""

return static_file(filename, root='static')

# 這裡匯入你的路由路勁

log_path = ('/'.join((base_dir, 'log')))

logging.basicconfig(level=logging.info,

format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',

datefmt='%y-%m-%d %h:%m:%s',

filename="%s/error_log" % log_path,

filemode='a')

host = '0.0.0.0'

port = sys.ar**[1] if len(sys.ar**) > 1 else '8080'

data = os.popen(f"netstat -ano| findstr :").read()

try:

process = data.split("\n")[0].split("listening")[1].replace(" ", "")

result = os.popen(f"taskkill /f -pid ").read()

print(result)

except:pass

run(server='tornado', host=host, port=port, reloader=true)

```python

from bottle_sqlalchemy import sessionmaker

from bottle_sqlalchemy import sqlalchemyplugin

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import create_engine

from bottle_plus import bottle_plus

import bottle_session

import bottle_redis

import redis

def db_config():

databases =

return 'mysql+{}://{}:{}@{}/{}?charset=utf8mb4&autocommit=true'.format(databases['engine'], databases['user'], databases['password'], databases['host'], databases['name'],)

base = declarative_base()

engine = create_engine(db_config(), echo=true, pool_size=100, pool_recycle=3600)

session = sessionmaker(autocommit=false, autoflush=true, bind=engine)

db = session()

if you want to use this model, you can follow these steps in view.py, or you can not allow this model.

'''

@route('/')

def test(session, rdb):

rdb.incr('visitors')

visitor_num = rdb.get('visitors')

# when cookie_lifetime=10, last_visit_time will be none

last_visit_time = session['visit']

session['visit'] = datetime.now().isoformat()

# first time when you login this website, you can setting a dict like follwing

session['name'] = '我愛你'

name = session.get('name')

return ''.format(visitor_num, last_visit_time, name)

'''session_plugin = bottle_session.sessionplugin(cookie_lifetime=10)

redis_plugin = bottle_redis.redisplugin(

)connection_pool = redis.connectionpool(host=

'127.0.0.1'

, port=

6379

)session_plugin.connection_pool = connection_pool

redis_plugin.redisdb = connection_pool

bottle

bottle_sqlalchemy

sqlalchemy

mysqlclient

pymysql

beaker

gevent

tornado

mysql-connector

接下來就開始我們探索bottle為框架的旅行吧

bottle微框架從註冊到應用(二) 註冊功能

為了節省 建議去bootstrap蕩吧,哈哈。這裡只貼表單了 大概就是這樣 功能方法 使用者名稱 w 手機號 1 3 9 d 密碼驗證兩次密碼是否一致 from sqlalchemy import column,integer,sequence,string,boolean,enum from ut...

Go從入門到框架

目錄推薦閱讀 來自 www.liwenzhou.com 001 為什麼你應該學習go語言?002 從零開始搭建go語言開發環境 003 vs code配置go語言開發環境 004 go語言基礎之變數和常量 005 go語言基礎之基本資料型別 006 go語言基礎之運算子 007 go語言基礎之流程控...

從微信授權到微信支付

水費 繳費 生成簽名演算法 5public static string createsign sortedmapparameters,string key string sign md5util.md5encode sb.tostring utf 8 touppercase return sign ...