SoFunction
Updated on 2024-11-12

Python's method of turning on a server via commands

preamble

If you need a simple Web Server urgently, but you don't want to download and install those complicated HTTP service programs, such as: Apache, ISS, etc.. Then Python may help you. A simple built-in HTTP server can be accomplished using Python. So, you can present all your directories and files as HTTP. The only thing you need to do is to install a Python.

And for those who are not familiar with installing python, you can refer to these two articles:

  • python3.5 in win10 environment
  • Installing the latest Python 3.6 version on Linux

Once installed you can begin the body of this article.

Python opens the server with the command

If you want to use the command E:\zpic as the directory to provide downloads, then cd to that directory in cmd and execute the command:

python -m SimpleHTTPServer

The default port number is 8000, and the server root directory is the working directory for running python commands

If an error is prompted:

No module named SimpleHTTPServer

Then try the command:

python -m 

Use http://127.0.0.1:8000Access to the contents of the catalog

Or add the port number:python -m   80

Use http://127.0.0.1Access to the contents of the catalog

The server is down:

1) dos

Type "ctrl + C" in the run screen to stop it

2) python script

You need to stop port listening and related python processes.

def stop_server(server)
()

========================

There are three types of modules used to build the http server as follows:

(1) BaseHTTPServer: provides basic Web services and processor classes , respectively, HTTPServer and BaseHTTPRequestHandler;

(2) SimpleHTTPServer: contains the SimpleHTTPRequestHandler class that performs GET and HEAD requests;

3) CGIHTTPServer: contains the CGIHTTPRequestHandler class that handles POST requests and execution.

summarize

Above is the entire content of this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for my support.