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

How to use nginx as a load balancer for mysql

Note: The nginx version must be 1.9 or above. Whe...

Thoughts on truncation of multi-line text with a "show more" button

I just happened to encounter this small requireme...

How to create users and manage permissions in MySQL

1. How to create a user and password 1. Enter the...

How to avoid garbled characters when importing external files (js/vbs/css)

In the page, external files such as js, css, etc. ...

Summary of learning HTML tags and basic elements

1. Elements and tags in HTML <br />An eleme...

Nest.js authorization verification method example

Table of contents 0x0 Introduction 0x1 RBAC Imple...

The whole process of configuring hive metadata to MySQL

In the hive installation directory, enter the con...

Python MySQL database table modification and query

Python connects to MySQL to modify and query data...

How to enable MySQL remote connection in Linux server

Preface Learn MySQL to reorganize previous non-MK...

Solution to the problem of repeated pop-up of Element's Message pop-up window

Table of contents 1. Use 2. Solve the problem of ...

Django uses pillow to simply set up verification code function (python)

1. Import the module and define a validation stat...

Explain the difference between iframe and frame in HTML with examples

I don't know if you have used the frameset at...

Vue implements the method example of tab routing switching component

Preface This article introduces the use of vue-ro...

Steps to install MySQL using Docker under Linux

As a tester, you may often need to install some s...