LoginSignup
5
3

More than 5 years have passed since last update.

[Python]BottleでさくっとWebアプリケーション!

Last updated at Posted at 2017-06-29

はじめに

「Webアプリケーションを作ってみたい…」と思い立ち、現在勉強中のPythonで良いフレームワークが無いかな?と調べてみたところ…

Bottleというものを見つけたので、Hello Wordまでの備忘録を。

動作環境

  • Amazon Linux AMI release 2017.03
  • Python 2.7.12

HelloWordまで

yum update

まぁ、基本だからやっておきましょうね。

# sudo yum update
Loaded plugins: priorities, update-motd, upgrade-helper
No packages marked for update 

うん、新しいサーバって素敵。

bottleインストール

色々調べた所、Bottle本体をwgetで配置したりとか…
でも、私は公式ドキュメントを信じる!!
1.png

# pip install bottle
You are using pip version 6.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting bottle
100% |******************************| 73kB 4.0MB/s 
Installing collected packages: bottle
  Running setup.py install for bottle
Successfully installed bottle-0.12.13

よし、特に無事に入ったッぽいね。

アプリ配置

Bottleのページのど真ん中にあるやつを使うんだな。
Example: “Hello World” in a bottle

適当な名前を付けて作っちゃおう。

main_app.py
from bottle import route, run, template

@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

run(host='localhost', port=8080)

実行権限付与

お忘れ無く!

# chmod +x ./main_app.py 

Runさせる

さぁ動かすぞ!

# python main_app.py 
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

お!Listenし始めた。
んじゃ、ターミナル複製してcurlしてみる。

$ curl http://localhost:8080/hello/jump
<b>Hello jump</b>!

こんちゃーっす!!

Runさせてたターミナルにはアクセスログが返ってきてます。

127.0.0.1 - - [29/Jun/2017 09:48:38] "GET /hello/jump HTTP/1.1" 200 18

おわりに

BottleのHelloWordまでやってみました。
公式ドキュメント読めばいいんじゃね?っていう声も聞こえてきそうですが…
誰かのお役に立てれば幸いです。

余談

とりあえず、Qiita初投稿という事で、練習がてら書いてみたけど、
Qiita素敵!!:relaxed:
今後とも続けていきたい!

5
3
1

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
5
3