LoginSignup
0
5

More than 5 years have passed since last update.

PythonでWeb framework を作ろう! (1)

Posted at

折角のお盆休みですので、PythonでWeb Frameworkを作成することにしました。
以前にも、PHPでRoRのデッドコピーなFrameworkを作ったことがあるので、今回はPythonで作ってみようというのが今回の趣旨です。

Hellow world!

まず、WorldにHellowしないと始まりません。
挨拶はとても重要ですからね。(謎)
下記のサイトを参考にさせていただきました。

EC2にnginx+uwsgi+python でHello World
http://qiita.com/chunkof/items/6c9d4b01f0057a9a8de0

ただ、このサイトの記述には抜けがあるようです。
まず、nginxの設定です。下記のとおりVirtualHostの設定をします。
nginxのインストールとファイル位置の記述は省きます。

strangerpy.conf
server{
    listen 80;
    server_name    strangerpy.example.com;

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3031;
    }

    location = /favicon.ico{
      empty_gif;
    }
}

次にuwsgiの設定ファイル

uwsgi.ini
[uwsgi]
master = True
socket = 127.0.0.1:3031
wsgi-file = index.py
stats = 127.0.0.1:9191
logto = uwsgi.log
pidfile = uwsgi.pid

プログラムのソースファイル

index.py
# index.py
# coding:utf-8
import logging
import datetime
import uuid

handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)

def main():
    # log
    logger.info('test-log-dayo')

def application(env, start_response):
    main()
    start_response('200 OK', [('Content-type', 'text/html')])
    str = "<html><head><meta charset='utf-8'><title>Home</title></head><body>はろーわーるど!</body></head>".encode("utf-8")
    return [str]

一通り記述したら、index.pyやuwsgi.iniをおいてあるディレクトリに移動しあ上で
下記の呪文を唱えてuwsgiを起動してください。

uwsgi --ini uwsgi.ini

/etc/hostsなどにサーバの名前解決の記述など書かないといけないのですが、それも省きました。
本論とは関係ないので。

http://strangerpy.example.com/ のURLを用いてブラウザで「はろーわーるど!」と出たら成功です。多分。

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