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

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

Docker data volume common operation code examples

If the developer uses Dockerfile to build the ima...

Native JavaScript to achieve skinning

The specific code for implementing skinning with ...

Facebook's nearly perfect redesign of all Internet services

<br />Original source: http://www.a-xuan.cn/...

Tutorial on using Multitail command on Linux

MultiTail is a software used to monitor multiple ...

MySQL UNION operator basic knowledge points

MySQL UNION Operator This tutorial introduces the...

Implementation code for adding slash to Vue element header

<template> <div class="app-containe...

Specific use of Linux gcc command

01. Command Overview The gcc command uses the C/C...

Detailed explanation of the execution process of MySQL query statements

Table of contents 1. Communication method between...

MySQL 8.0.16 installation and configuration tutorial under CentOS7

Uninstall the old version of MySQL (skip this ste...

JavaScript to implement voice queuing system

Table of contents introduce Key Features Effect d...

Detailed explanation of Vue-Jest automated testing basic configuration

Table of contents Install Configuration Common Mi...

How to optimize MySQL group by statement

In MySQL, create a new table with three fields, i...

Detailed explanation of creating stored procedures and functions in mysql

Table of contents 1. Stored Procedure 1.1. Basic ...