Analysis of the process of building a LAN server based on http.server

Analysis of the process of building a LAN server based on http.server

I don’t know if you have ever encountered such a situation. When doing a project or research and development, you urgently want to transfer a file to another computer, but you can’t find the USB drive, so you have to log in to social software such as QQ, WeChat, or use tools such as email to transfer it. It is very troublesome and distressing. It is said that Python is omnipotent, and can do everything except giving birth to children! So today I will introduce a simple way to solve this problem. By calling http.server in Python to build a LAN server, this problem becomes so easy!

1.

How to build a local area network with one line of Python command is actually very simple. We can do it with one command in Python! According to Python's official manual, HTTPServer is a subclass of TCPServer in socketserver. It creates and listens on an HTTP socket and dispatches requests to the handler.

In Python 3.7, http.server provides 5 parameters. Calling the following help command will display the following: python -m http.server --help

As can be seen from the figure above, it contains positional parameters and optional parameters.

  • Position parameter port - refers to the port specified by the HTTP server, the default is 8000
  • Optional parameter -h/--help——used to get help information
  • --cgi——The server is in CGI mode
  • --bind/-b ADDRESS——Bind ip address, ADDRESS is the ip address
  • --directory/-d DIRECTORY——Specify the directory, the default is the current directory (that is, the directory where the command line is run). Simply put, the current server can provide access to the directory

2. How to operate

The code for building a local server is very simple. You only need to run the following code in the command line.

python -m http.server 8080

The above code is used to start the http server. The default IP is the local wireless IPv4 network of the computer, and the default directory is the directory where the command line is run. If you want to change the parameters, call 03 types of variable parameters.

The explanation of the principle ends here. The next step is practice. The editor used the hotspot of his own mobile phone to test it on Ubuntu, Windows and mobile terminals.

1). First, you need to obtain the local IP

The Windows platform can obtain the IP address through ipconfig (the Linux platform can obtain the IP address through ifconfig -a). Note that the IP address selected is the IPv4 address in the wireless LAN adapter WLAN.

2).

After starting the server with the command above, you can access the server by entering http://192.168.43.136:8080/ in the browser according to the obtained IP and port. Isn't it amazing? Next, let's test it on different platforms and try it on PC:

Test it with your phone

Other Windows

3.

Change the directory of the server. For changing the file directory, the editor changes the D drive to the accessible drive. The command is as follows. The usage of other parameters is similar. python -m http.server 8080 -dd: You can see that the accessed directory has changed:

That’s all for today’s sharing of tips on using http.server to build a server. Have you learned something new? Using this small function that comes with Python, you can quickly build a simple http server so that all devices in the LAN can access it.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • [Asp.Net Core] Implement image verification code with Blazor Server Side
  • [Asp.Net Core] A brief discussion on Blazor Server Side
  • Ant Design Blazor component library's routing reuse multi-tab function
  • Detailed explanation of header information in HTTP
  • Golang simply implements the server and client side of http
  • IOS uses CocoaHttpServer to build a local server on the mobile phone
  • Golang implements http server to provide compressed file download function
  • Using http.FileServer in Golang to return static files
  • Detailed explanation of golang's httpserver elegant restart method
  • Making HTTP requests in Blazor Server applications

<<:  Troubleshooting the reasons why MySQL deleted records do not take effect

>>:  Javascript common higher-order functions details

Recommend

Concat() of combined fields in MySQL

Table of contents 1. Introduction 2. Main text 2....

Summary of MySQL basic common commands

Table of contents MySQL basic common commands 1. ...

A brief discussion on the synchronization solution between MySQL and redis cache

Table of contents 1. Solution 1 (UDF) Demo Case 2...

Why MySQL should avoid large transactions and how to solve them

What is a big deal? Transactions that run for a l...

Detailed introduction to MySQL database index

Table of contents Mind Map Simple understanding E...

How to use vue filter

Table of contents Overview Defining filters Use o...

Pure CSS3 to create page switching effect example code

The one I wrote before is too complicated, let’s ...

Practical record of solving MySQL deep paging problem

Table of contents Preface Why does limit deep pag...

js array fill() filling method

Table of contents 1. fill() syntax 2. Use of fill...

SQL merge operation of query results of tables with different columns

To query two different tables, you need to merge ...

Solution to the failure of entering the container due to full docker space

Since the problem occurred rather suddenly and th...

Two ways to use react in React html

Basic Use <!DOCTYPE html> <html lang=&qu...