LoginSignup
1
1

More than 3 years have passed since last update.

python Bottleの基本1(Hello World)

Posted at

Bottleとは、とてもコンパクトWeb Server(WSGI)です。

WSGI(Web Server Gateway Interface)とは、サーバーとWebアプリケーションをつなぐ共通のインターフェースをPythonで定義したもです。
python言語で作成できるWeb serverで簡単にユーザーインターフェイスを構築できます。
image.png

ex1.py
# bottleの基本1
from bottle import *
@route('/')
def home():
    return "<h1>Hello World</h1>"
HOST,PORT='localhost',8080 #web server のアドレスとポートの定義
if __name__ =='__main__':
    run(host=HOST,port=PORT)

上記のソースコードは、bottle Serverの最低限のコードです。

@route('/')は、def home()の関数に対するデコレーション(機能追加)です。
本来ならば、
def home():
     ......
route('/','GET',home)
デコレーション機能を使ってすっきり書くことができます。

@付いたらデコレーション関数だ。

image.png

HOST,PORT='localhost',8080 #web server(WSGI)
このコーディングでHOSTとPORTを定義しています。
他の言語を学習した方は、違和感あるでしょうが=の意味を考えてみましょう。
多くの場合、=は、Moveとして習慣化されています。
A=10とコーデングしたなら10をAに移動する(load)となりますが、
pythonの場合は、10の値にAという名前(alias)を付けるという意味になります。
インタープリタの立場にたって考えるとわかりやすいです。

たった8行でWeb Serverを構築できます。

1
1
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
1
1