How to specify a public directory Python simple HTTP server

Python has a simple HTTP server function for web development. It's convenient, but it's inconvenient because I have to put the public script directly under the directory I want to publish (I thought), and I just found out how to specify the public directory.

Conclusion

(Supports Python 3.7 and above) Below, you can enter the relative path from the script in __DIRECTORY.

Quoted from Reference 1

server.py


import http.server
import socketserver

PORT = 8000
DIRECTORY = "web"

class Handler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=DIRECTORY, **kwargs)


with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

Reference 1

StackOverflow "How to run a http server which serves a specific path?" I just wrote what I wanted to know.

Code meaning

If you do not specify a public directory, you can use the following code. The directory where the script exists automatically becomes the public directory. By passing a handler to TCPServer () as an argument, the appropriate argument is put in the constructor of SimpleHTTPRequestHandler in TCPServer (). So this __Handler is an image like a function pointer __. (That is, is it a "handler"?)

Do not specify a directory.py



Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

So, what we are doing in the code of the above conclusion is to call the constructor of SimpleHTTPRequestHandler in TCPServer () without changing the outline (* args and ** kwargs in the code below), The __constructor is overwritten so that "arbitrary specified directory" is passed to the argument directory.

I investigated only the difficult parts.py



#Redefine the initialization function of SimpleHTTPRequestHandler
class Handler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=DIRECTORY, **kwargs)

#I really want to do this, but I get an error with insufficient arguments.
#The argument is TCP Server()I can't put it in here & I don't know because I put it properly inside.
Handler = http.server.SimpleHTTPRequestHandler(directory="web")

#This is no good either. Naturally, it is said what args are.
Handler = http.server.SimpleHTTPRequestHandler(*args,directory="web",**kwargs)

Reference 2

From python specifications http.server image.png

so! All I want to do is put the arguments in the following directories! image.png

Reference 3

Let's init a derived class using inheritance and super () in python I didn't understand the part of overwriting the constructor, so I was taken care of. How to use Python variable length arguments (* args, ** kwargs)

Recommended Posts

How to specify a public directory Python simple HTTP server
Simple HTTP Server for python
A simple Python HTTP server that supports Range Requests
How to create a simple TCP server / client script
How to write a Python class
[Mac] I want to make a simple HTTP server that runs CGI with Python
[Python Kivy] How to create a simple pop up window
How to set up a simple SMTP server that can be tested locally in Python
A simple HTTP client implemented in Python
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
[Python] How to invert a character string
How to get a stacktrace in python
How to run a Maya Python script
Send a message from Slack to a Python server
Set up a simple HTTPS server in Python 3
How to read a CSV file with Python 2/3
A simple example of how to use ArgumentParser
How to create a Python virtual environment (venv)
How to clear tuples in a list (Python)
How to embed a variable in a python string
Start a simple Python web server with Docker
How to generate a Python object from JSON
How to add a Python module search path
How to specify TLS version in python requests
How to specify attributes with Mock of python
How to notify a Discord channel in Python
How to set up a local development server
Set up a simple SMTP server in Python
[Python] How to draw a histogram in Matplotlib
How to read a file in a different directory
How to Mock a Public function in Pytest
How to get a list of files in the same directory with python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
How to install Python
How to install python
How to specify a schema in Django's database settings
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
How to set up a Python environment using pyenv
How to know the current directory in Python in Blender
[Reintroduction to python] How to import via the parent directory
[Python] How to expand variables in a character string
[Python] How to specify the download location with youtube-dl
How to write a list / dictionary type of Python3
How to build a Django (python) environment on docker
How to make a Python package using VS Code
[Python] How to write a docstring that conforms to PEP8
[Python] How to split and modularize files (simple, example)
How to save a table scraped by python to csv
[Python] Summary of how to specify the color of the figure
[Python] How to create a 2D histogram with Matplotlib
How to execute a command using subprocess in Python
How to run Django on IIS on a Windows server
How to build a Python environment on amazon linux 2
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
[Python] How to draw a scatter plot with Matplotlib
[Vagrant] Set up a simple API server with python
How to host web app backend processing in Python using a rental server subdomain
How to use GitHub on a multi-person server without a password