Implement a simple application with Python full scratch without using a web framework.

Introduction

It's now easier to develop web apps using web frameworks like Django and Flask, but it doesn't really make sense to just learn how to use the framework without understanding the roots. , I decided to understand the mechanism by implementing a simple blog app using only python's built-in functions and standard library without using the web framework.

Creating an index page

Since the web server returns (displays) something when accessed from a web browser, create the index page to be displayed first in html. I created an index page that outputs Hello, World !.

index.html


<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>Hello, World!</p>
</body>
</html>

Creating an HTTP server

webserver.py


from http.server import HTTPServer, SimpleHTTPRequestHandler

def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()


if __name__ == '__main__':
    run()

HTTP Server is one of the standard python libraries for implementing web server functionality. In the HTTPServer argument, specify the server name and port number tuple and request handler. First of all, as a test, as an argument of HTTPServer, specify the server name (default is localhost) and the port number (identification number of the accessing program) 8000, and it is one of the request handlers that manages requests from clients. It specifies two SimpleHTTPRequestHandlers (simple classes that return files that correspond to client access).

Start HTTP server

Terminal


$ python webserver.py

When I access localhost: 8000 with a browser, Hello, World is output.

HTTP server notes

If you refer to the official documentation [^ 1], there is a warning that it is not suitable for http.server commercial use.

Warning http.server is not recommended for production. It only implements basic security checks.

Since the built-in server is slow and cannot withstand the load, it is common to use a web server application such as Apache or Nginx for commercial use. This time, the purpose is to understand and implement the mechanism of the web application, so we will use the built-in server as it is.

Change the processing of the request handler

In the HTTP server executed in the test earlier, SimpleRequestHandler was specified as the request handler, but since this was a process that only returned index.html, next we will add our own process.

Override the ability to display the formatted string in html in SimpleRequestHandler to the do_GET method. In the first place, the do_GET method performs the process of returning an HTTP response and returning the corresponding file.

webserver.py


with open('index.html', 'r')as f:
    index_file = f.read()

class OriginalHTTPRequestHandler(SimpleHTTPRequestHandler):
    def do_GET(self):

        self.send_response(200)
        self.end_headers()

        html = index_file.format(
            title = 'Blog title',
            content = 'Blog content'
        )
        self.wfile.write(html.encode('utf-8'))
        return None

Fix the part where Hello, World was hard-coded in the html file so far.

index.html


<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>{title}</h1>
    <p>{content}</p>
</body>
</html>

When you start the server and access localhost: 8000, you can see that it is displayed as follows.

Blog title

Blog content

Realize the posting function by submitting a form

Now you can display the formatted string in html, but since the blog has a posting function and it is assumed that you can see the post list, next post using HTML form tags Implement the function. Rewrite index.html as follows.

index.html


<body>
    <h1>{title}</h1>
    <p>{content}</p>

    <form method = 'POST' action="/result.html">
        <textarea name="titlefield" id="titlefield" cols="30" rows="3" maxlength=15 placeholder="enter title"></textarea><br>
        <textarea name="contentfield" id="contentfield" cols="30" rows="10" placeholder="Enter the content of the post"></textarea><br>
        <input type="submit"><br>
    </form>
</body>

Change the method of the form tag to'POST' and the action to'result.html' to display the post result. Create result.html to display the post result.

result.html


<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>{header}</h1>
    <h2>{title}</h2>
    <p>{content}</p>
    <p><a href="{link}">Return to the posting screen</a></p>
</body>
</html>

Then modify the server program. Add a do_POST () method to process the submitted Form information. Utilize the FieldStorage class of the cgi module to process the value submitted by the Form. FieldStorage is an object that manages the information submitted in the form.

webserver.py


from cgi import FieldStorage
from http.server import HTTPServer, SimpleHTTPRequestHandler

with open('index.html', 'r') as f:
    index_file = f.read()

with open('result.html', 'r') as f:
    result_file = f.read()


class OriginalHTTPRequestHandler(SimpleHTTPRequestHandler):
    def do_GET(self):

        self.send_response(200)
        self.end_headers()

        html = index_file.format(
            header = 'Post screen',
            title = 'Blog title',
            content = 'Blog content'
        )
        self.wfile.write(html.encode('UTF-8'))
        return None
    

    def do_POST(self):

        form = FieldStorage(
            fp = self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST'})

        title_form = form['titlefield'].value
        content_form = form['contentfield'].value
        

        self.send_response(200)
        self.end_headers()

        html = result_file.format(
            header = 'Posting result',
            title_message = 'title:',
            content_message = 'Posted content:',
            title = title_form, 
            content = content_form,
            link = '/result.html'
        )
        self.wfile.write(html.encode('utf-8'))
        return None

def run(server_class=HTTPServer, handler_class=OriginalHTTPRequestHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()


if __name__ == '__main__':
    run()

When I run the server and access localhost: 8000 with a browser, the Form tag is output.

スクリーンショット 2020-04-03 15.07.21.png You can now view the posted results by pressing the submit button. スクリーンショット 2020-04-03 15.07.53.png # Connect to database However, as it is, the title and posted content will be overwritten each time you post. One way to solve this problem is to save the input contents of the Form tag to a text file or a database, but this time I will save it to the database.

The database that can be handled by the standard library of python is Sqlite3 or DBM that can be handled like a file operation, so from the next time onward, we will use Sqlite3 of RDB to save the data.

Reference material

[^ 1]: Python official documentation --- HTTP server

Recommended Posts

Implement a simple application with Python full scratch without using a web framework.
[Python] A quick web application with Bottle!
Run a Python web application with Docker
Let's make a web framework with Python! (1)
Let's make a web framework with Python! (2)
Start a simple Python web server with Docker
(Python) Try to develop a web application using Django
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Implement a circular expression binary search in Python. There is a comparison with a simple full search.
Creating a web application using Flask ②
Build a web application with Django
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
If you know Python, you can make a web application with Django
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
I tried to make a todo application using bottle with python
Let's make a web chat using WebSocket with AWS serverless (Python)!
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Creating a simple PowerPoint file with Python
Install Python as a Framework with pyenv
Try using the web application framework Flask
Daemonize a Python web app with Supervisor
Create a simple web app with flask
I tried benchmarking a web application framework
I made a simple blackjack with Python
I made a WEB application with Django
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
I want to make a web application using React and Python flask
Put Docker in Windows Home and run a simple web server with Python
Let's make an A to B conversion web application with Flask! From scratch ...
I tried to make a simple mail sending application with tkinter of Python
Create a web application execution environment of Python3.4 + Nginx + uWSGI + Flask with haste using venv on Ubuntu 14.04 LTS
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
Create a simple reception system with the Python serverless framework Chalice and Twilio
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Try using the Python web framework Tornado Part 1
Create a web map using Python and GDAL
Steps to develop a web application in Python
[Python] Make a simple maze game with Pyxel
Build a simple Python virtual environment without pyenv
Launch a web server with Python and Flask
A simple to-do list created with Python + Django
Try using the Python web framework Tornado Part 2
Create a Python console application easily with Click
Extract data from a web page with Python
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[Python] Create a ValueObject with a complete constructor using dataclasses
Make a simple Slackbot with interactive button in python
Let's make a simple game with Python 3 and iPhone
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Register a ticket with redmine API using python requests
Web application made with Python3.4 + Django (Part.1 Environment construction)
Build a machine learning application development environment with Python
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
What is God? Make a simple chatbot with python
Using a Python program with fluentd's exec_filter Output Plugin
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[Vagrant] Set up a simple API server with python