I made a login / logout process using Python Bottle.

I wanted to make it a little, so I looked it up. The following page was used as a reference

https://qiita.com/Gen6/items/c153d562e757d88aa5c1 https://stackoverflow.com/questions/35588873/how-to-logout-in-python-bottle http://www.denzow.me/entry/2017/12/09/103828 https://qiita.com/yoskmr/items/8d35b6c7a15cfa275dfc

code

It looks like this. Most of the scripts on the reference page were diverted. Thanks.

!/user/bin/env python
 -*- coding: utf-8 -*-

from bottle import route, run, template, request, static_file, url, get, post, response, error
from bottle import redirect
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

SECRET_KEY = 'some-secret-key'
LIFE_TIME = 120

@route("/")
def html_index():
    user_id = request.get_cookie('account', secret=SECRET_KEY)
    if user_id is None:
        redirect('/login')
    else:
        return template('index',url=url)


@route("/static/<filepath:path>", name="static_file")
def static(filepath):   
    user_id = request.get_cookie('account', secret=SECRET_KEY)
    if user_id is None:
        redirect('/login')
    else:
        return static_file(filepath, root="./static")


@get("/login")
def login():
    return """
        <form action="/login" method="post">
            Username: <input name="username" type="text" />
            Password: <input name="password" type="password" />
            <input value="Login" type="submit" />
        </form>
    """

@route("/login", method="POST")
def do_login():
    username = request.forms.get("username")
    password = request.forms.get("password")
    if check_login(username, password):
        response.set_cookie("account", username, secret=SECRET_KEY, path='/', max_age=LIFE_TIME )
        redirect('/')
    else:
        redirect('/login')

def check_login(username, password):
  if username == "admin" and password=="password":
    return True
  else:
    return False


@route('/logout')
@route('/logout', method="POST")
def logout():
    response.delete_cookie('account')
    redirect('/login')

@error(404)
def error404(error):
    return template("404")

run(host="localhost", port=8080, debug=True, reloader=True)

with this, -The initial state transitions to the login screen. ・ After logging in, register user information in a cookie, and then access it afterwards. -When you access / logout, the cookie information is deleted and you are logged out. ・ The cookie becomes invalid after the specified time. I was able to do it.

The directory structure looks like this.

├─static
│  ├─css
│  ├─img
│  └─js
└─views

You should put the file used in template under views. The template is explained in detail on this page, so please refer to it here. http://www.denzow.me/entry/2018/03/03/220942

It seems to be very convenient because micro Python can be used in Template.

Does this method follow the general practice? ?? ?? It's a lot difficult.

Recommended Posts

I made a login / logout process using Python Bottle.
I made a Line-bot using Python!
I made a python text
I made a fortune with Python.
I made a daemon with Python
I made a quick feed reader using feedparser in Python
I made a Chatbot using LINE Messaging API and Python
I made a payroll program in Python!
I made a character counter with Python
Beginner: I made a launcher using dictionary
I tried to make a todo application using bottle with python
I made a Hex map with Python
I made a scaffolding tool for the Python web framework Bottle
I made a poker game server chat-holdem using websocket with python
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I tried to create a sample to access Salesforce using Python and Bottle
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I tried reading a CSV file using Python
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Python Qiita API wrapper "qiipy"
〇✕ I made a game
Daemonize a Python process
I made a script to record the active window using win32gui of Python
I made a prime number generation program in Python
I made a Christmas tree lighting game with Python
I made a net news notification app with Python
I made a VM that runs OpenCV for Python
I made a Python module to translate comment outs
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
[Python] I made a classifier for irises [Machine learning]
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
I made a prime number generation program in Python 2
I made a python library to do rolling rank
I made a school festival introduction game using Ren’py
I tried using Python (3) instead of a scientific calculator
I made blackjack with python!
[Python] I tried using OpenPose
I made a discord bot
I made blackjack with Python.
I made wordcloud with Python.
I made a package to filter time series with python
[VSCode] I made a user snippet for Python print f-string
I made a simple book application with python + Flask ~ Introduction ~
I made a VGG16 model using TensorFlow (on the way)
I made a puzzle game (like) with Tkinter in Python
I tried to make a stopwatch using tkinter in python
I made a muscle training estimation app using Qore SDK