Let's make a web framework with Python! (1)

Since it's a special holiday, I decided to create a Web Framework with Python. I've made a dead copy framework of RoR with PHP before, so this time I'm trying to make it with Python.

Hellow world! First of all, it will not start unless you Hello to World. Greetings are very important. (Mystery) I referred to the following site.

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

However, there seems to be a gap in the description on this site. First, the nginx settings. Set up VirtualHost as follows. I will omit the installation of nginx and the description of the file location.

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;
    }
}

Next uwsgi config file

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

Program source file

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>Hello World!</body></head>".encode("utf-8")
    return [str]

After writing the whole thing, move to the directory where index.py and uwsgi.ini are stored and go to the above Start uwsgi by casting the following spell.

uwsgi --ini uwsgi.ini

I have to write the description of server name resolution in / etc / hosts etc., but I omitted that as well. Because it has nothing to do with this paper.

If you use the URL of http://strangerpy.example.com/ and the browser says "Hello World!", You are successful. Perhaps.

Recommended Posts

Let's make a web framework with Python! (1)
Let's make a web framework with Python! (2)
Let's make a GUI with python.
Let's make a graph with python! !!
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a Twitter Bot with Python!
Make a fortune with Python
Let's replace UWSC with Python (5) Let's make a Robot
Let's make a web chat using WebSocket with AWS serverless (Python)!
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
[Super easy] Let's make a LINE BOT with Python.
Let's make a breakout with wxPython
Make a recommender system with python
Let's make a supercomputer with xCAT
If you want to make a discord bot with python, let's use a framework
Let's make a websocket client with Python. (Access token authentication)
Install Python as a Framework with pyenv
Let's create a free group with Python
Daemonize a Python web app with Supervisor
Let's make a simple language with PLY 1
[Python] A quick web application with Bottle!
[Python] Let's make matplotlib compatible with Japanese
Run a Python web application with Docker
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a combination calculation in Python
Make a desktop app with Python with Electron
If you know Python, you can make a web application with Django
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Django python web framework
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
Introduction to Tornado (1): Python web framework started with Tornado
I want to make a game with Python
Start a simple Python web server with Docker
[Python] Make a simple maze game with Pyxel
Launch a web server with Python and Flask
Let's do web scraping with Python (weather forecast)
Let's make a module for Python using SWIG
Let's do web scraping with Python (stock price)
Extract data from a web page with Python
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
[Streamlit] I hate JavaScript, so I make a web application only with Python
Implement a simple application with Python full scratch without using a web framework.
Make one repeating string with a Python regular expression.
Web scraping with python + JupyterLab
Let's make an A to B conversion web application with Flask! From scratch ...
Try to make a command standby tool with python
Let's run Excel with Python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Make a simple Slackbot with interactive button in python
Let's make Othello with wxPython
Make a breakpoint on the c layer with python
Launch a Python web application with Nginx + Gunicorn with Docker
Make Puyo Puyo AI with Python
Web API with Python + Falcon
Let's make dice with tkinter