0
0

More than 1 year has passed since last update.

python3: bottle の使い方 (その 2)

Last updated at Posted at 2020-11-11

こちらにあるスニペットを実行できる形にしました。
ログインのサンプルになります。
Docs » チュートリアル

login.py
#! /usr/bin/python
#
#	login.py
#
#						Nov/11/2020
# ------------------------------------------------------------------
from bottle import get, post, request, run
#
def check_login(name,password):
	rvalue = True
	if name != password:
		rvalue = False
	return rvalue
#
# ------------------------------------------------------------------
@get('/login') # or @route('/login')
def login_form():
	str_out =  '<form method="POST" action="/login">'
	str_out += '<input name="name"	 type="text" />'
	str_out += '<input name="password" type="password" />'
	str_out += '<input type="submit" />'
	str_out += '</form>'

	return str_out
#
# ------------------------------------------------------------------
@post('/login') # or @route('/login', method='POST')
def login_submit():
	name	 = request.forms.get('name')
	password = request.forms.get('password')
	if check_login(name, password):
		return "<p>*** Your login was correct ***</p>"
	else:
		return "<p>*** Login failed ***</p>"
#
# ------------------------------------------------------------------
run(host='localhost', port=8080, debug=True)
#
# ------------------------------------------------------------------

サーバーの実行

$ ./login.py 
Bottle v0.12.19 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

<frozen importlib._bootstrap>:914: ImportWarning: _ImportRedirect.find_spec() not found; falling back to find_module()

ブラウザーで、http://localhost:8080/login
にアクセス
login.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0