Leichtgewichtige Web-Frameworks: Flask, Falcon, Bootle

Flask

# Python in a nutshell, 3rd edition

import datetime, flask

app = flask.Flask(__name__)

app.permanent_session_lifetime = datetime.timedelta(days = 365)
# secret_key wird für die Session benötigt
app.secret_key = b'\xc5\x8f\xbc\xa2\x1d\xeb\xb3\x94;:d\x03'

@app.route('/')
def greet():
    lastvisit = flask.session.get('lastvisit')
    now = datetime.datetime.now()
    newvisit = now.ctime()
    template = '''
      <html><head><title>Hello, visitor!</title>
      </head><body>
      {% if lastvisit %}
        <p>Welcome back to this site!</p>
        <p>You last visited on {{lastvisit}} UTC</p>
        <p>This visit on {{newvisit}} UTC</p>
      {% else %}
        <p>Welcome to this site on your first visit!</p>
        <p>This visit on {{newvisit}} UTC</p>
        <p>Please Refresh the web page to proceed</p>
      {% endif %}
      </body></html>'''
    flask.session['lastvisit'] = newvisit
    return flask.render_template_string(template, newvisit = newvisit, lastvisit = lastvisit)

@app.route('/hello')
def hello():
    page = '''
      <html><head><title>Hello, visitor!</title>
      </head><body>
      Hello World!
      </body></html>'''
    return page

app.run(debug = True)

Bottle

# Python in a nutshell, 3rd edition
#
# Test-Aufrufe:
#
# curl http://127.0.0.1:8080/
# curl http://127.0.0.1:8080/hello
# curl http://127.0.0.1:8080/bye

# Bottle Tutorial
# https://bottlepy.org/docs/dev/tutorial.html

import datetime, bottle
from bottle import SimpleTemplate, template

one_year = datetime.timedelta(days = 365)

@bottle.route('/hello')
def hello():
    t = SimpleTemplate('Hello {{name}}!')
    return t.render(name = 'World')

@bottle.route('/bye')
def bye():
    return template('Bye-bye {{name}}!', name='World')

@bottle.route('/')
def greet():
    lastvisit = bottle.request.get_cookie('lastvisit')
    now = datetime.datetime.now()
    newvisit = now.ctime()
    thisvisit = '<p>This visit on {} UTC</p>'.format(newvisit)
    bottle.response.set_cookie('lastvisit', newvisit, expires = now + one_year)
    resp_body = ['<html><head><title>Hello, visitor!</title></head><body>']
    if lastvisit is None:
        resp_body.extend((
          '<p>Welcome to this site on your first visit!</p>',
          thisvisit,
          '<p>Please Refresh the web page to proceed</p>'))
    else:
        resp_body.extend((
          '<p>Welcome back to this site!</p>',
          '<p>You last visited on {} UTC</p>'.format(lastvisit),
          thisvisit))
    resp_body.append('</body></html>')
    bottle.response.content_type = 'text/html'
    return resp_body

if __name__ == '__main__':
    bottle.run(debug = True)

Falcon

# Python in a nutshell, 3rd edition
#
# s.a. http://www.giantflyingsaucer.com/blog/?p=4342
#
# Start:
# uwsgi --http :8080 --wsgi-file falcon1.py --callable app

import datetime, falcon
one_year = datetime.timedelta(days = 365)

class Greeter(object):
    def on_get(self, req, resp):
        lastvisit = req.cookies.get('lastvisit')
        now = datetime.datetime.now()
        newvisit = now.ctime()
        thisvisit = '<p>This visit on {} UTC</p>'.format(newvisit)
        resp.set_cookie('lastvisit', newvisit, expires= now + one_year, secure = False)
        resp_body = ['<html><head><title>Hello, visitor!</title></head><body>']
        if lastvisit is None:
            resp_body.extend((
            '<p>Welcome to this site on your first visit!</p>',
            thisvisit,
            '<p>Please Refresh the web page to proceed</p>'))
        else:
            resp_body.extend((
            '<p>Welcome back to this site!</p>',
            '<p>You last visited on {} UTC</p>'.format(lastvisit),
            thisvisit))
        resp_body.append('</body></html>')
        resp.content_type = 'text/html'
        resp.body = ''.join(resp_body)
        #resp.status = falcon.HTTP_200

app = falcon.API()
greet = Greeter()
app.add_route('/', greet)