廉价VPS跑小站的框架选择bjoern+Bottle

bjoern很稳定,搭配Bottle 还不错。但被流量攻击时会出现cpu 100%的情况,解决办法是定时重启。

bjoern+Bottle示例

from bottle import route, run
           
@route('/')
def index():
    return 'hello world'
           
run(server='bjoern', host='0.0.0.0', port=8040)

bjoern+Bottle示例2

import os
port = int(os.environ.get('PORT', 7777))

import bjoern
from application import app
bjoern.run(app, '0.0.0.0', port)

下图是有人做的一个helloword比较,仅供参考。

图片:python-web.jpg

import bjoern
           
def start():
    def return_hello(environ, start_response):
        start_response('200 OK', [('Content-Type','text/plain')])
        return ('Hello world')
           
    def return_404(environ, start_response):
        start_response('404 Not Found', (('Content-Type','text/plain'), ))
        return "URL %s not found" % environ.get('PATH_INFO', 'UNKNOWN')
           
    dispatch = {
        '/': return_hello
    }
           
    def choose(environ, start_response):
        return dispatch.get(environ.get('PATH_INFO'), return_404)(environ, start_response)
    bjoern.run(choose, '0.0.0.0', 8081)
           
if __name__=="__main__"
    start()

bjoern 项目地址 https://pypi.python.org/pypi/bjoern https://github.com/jonashaag/bjoern 1

Relative Articles