Use line commands in Python to build an HTTP server and access it through the external network

1 Introduction

As a relatively popular programming language, Python has simple syntax and clear statements. Python has good compatibility and can be easily connected with modules established in other programming languages ​​(such as C/C++). Moreover, Python is rich and powerful. The library is encapsulated and can be easily called, so it is very popular.

Today we will try to use python to build a simple http server to display the specified directories and files on the local computer.

2. Set up local http server

Generally speaking, Python's simple http server does not rely on complex server programs such as Apache and IIS because it comes with only one line of commands to create an http service. But in contrast, this simple http service cannot provide complex functions and can only provide relatively simple directory services. But for those who want to learn and become familiar with python, this is another step that must be taken.

2.1.Python installation and setup

First, we need to install the python program locally. The Python program can be downloaded from its official website ( www.python.org/ ) to find the corresponding operating system version. The author is using the Windows operating system, so I chose the Windows version to download.

20230330132801

20230330132802

After the Python download is completed, click the installer.exe directly to install it. At the same time, it should be noted that during the installation process, you need to pay attention to checking Add python.exe to PATHthe options.

20230330132803

20230330132804

After completing the installation of the python program, we can start setting up the python http server.

2.2.Python server setup and testing

Since python has a built-in simple http service package, for python, you only need to enter a line of commands to easily open the http service. Of course, to run a web page, it needs to have display content. So we can first create a folder to store web page files. For example, the author created a new "test" folder under the E drive of the local computer.

20230330132805

Then, run the interface of the local computer as an administrator 命令提示符and enter the command to transfer the hard disk where the file is stored.

e:

Then go to the folder you want to share

cd test

20230330132806

20230330132807

Then enter the command启动http服务

  • If the Python version is 2.x, enter the command
python -m SimpleHTTPServer 8081
  • If the Python version is 3.x, enter the command
python -m http.server 8001

8081 is the output port of the server you plan to build. Just choose an unoccupied port. If a firewall prompt appears, just select 允许访问it.

After the command line is entered, python gives feedback Serving HTTP on 0.0.0.0 port 8081 ..., which means that our python http service has been successfully opened under the local port 8081.

20230330132808

Then we enter in the browser address bar on the local computer localhost:8081to open the file in the folder we intend to share.

20230330132809

If you can see these contents, it means that the python http service on the local computer has been opened, and the files in this folder can already be viewed. Similarly, if we put a web page file in this folder, it can also be displayed as a web page.

At this time, for devices under the same LAN, as long as you enter the local computer's IP address + port number in the browser (the specific format is 192.168.XXX.XXX:port number), the Python web page will be displayed.

20230330132810

However, web pages that cannot be accessed on the public Internet always feel lack of soul, so we can use cpolar intranet penetration to create an intranet penetration data tunnel, so that we can access Python under public Internet conditions.

3.Installation and registration of cpolar

Similarly, we can find the software corresponding to the operating system version on cpolar’s ​​official website ( https://www.cpolar.com/ ). The author here also chooses the Windows version.

20230330132811

20230330132812

After the Cpolar software is downloaded, unzip the compressed package and double-click it to .msi文件install it automatically. We only Nextneed to click all the way.

20230330132813

20230330132814

Since cpolar will create an independent data tunnel for each user, supplemented by user passwords and token codes to ensure data security, we need to register users before using cpolar. The registration process is also very simple. Just click on the upper right corner of the cpolar homepage 用户注册and fill in the necessary information on the registration page to complete the registration.

20230330132815

20230330132816

3.1 Cpolar cloud settings

After completing the installation and registration of cpolar, we can start using cpolar to create an intranet penetration data tunnel to connect the Python web page of the local computer to the public Internet. It should be noted that the data tunnel of the free version of cpolar is reset every 24 hours. The author does not want to reset the data tunnel settings every day, so I upgraded cpolar to the VIP version so that I can obtain an intranet penetration data tunnel that can exist stably for a long time.

To generate a long-term stable intranet penetration data tunnel, we must first visit the cpolar official website and log in, and on the left side of the "Dashboard" page, find and click the button to enter the "Reservation" page 预留.

20230330132817

20230330132818

In the "Reservation" page, we can see that cpolar provides data tunnel reservation projects for multiple protocols. The python server is http protocol, so we find 保留二级子域名the field. Of course, if you have purchased your own domain name from a domain name provider, you can also select the "Reserve custom domain name" field.

In the "Reserved second-level subdomain name" field, we need to set the information for the second-level subdomain name we intend to reserve, and the information set up will also become the tunnel entrance for us to access the local python server on the public Internet (the public Internet address part). The specific information that needs to be set is:

  • 地区: The area where the server is located, just select the nearest one
  • 二级域名will eventually appear in the generated public Internet address as one of the identifiers of the network address
  • 描述It can be regarded as a description of this data tunnel, which can be distinguished from other tunnels.

After entering these settings, you can click 保留the button on the right to retain this data tunnel. If we no longer want the entrance to this tunnel, we can also click the "x" on the right to easily delete the tunnel.

20230330132819

3.2 Cpolar local settings

After completing the cpolar cloud settings, we return to the local computer, open and log in to the cpolar client (you can enter localhost:9200 in the browser to access directly, or you can click the cpolar client shortcut in the start menu).

20230330132820

隧道管理Click the button on the left side of the client's main interface 创建隧道to enter the local tunnel creation page (if you want to create a temporary data tunnel that resets the address every 24 hours, you can set it up directly here without setting up a blank data tunnel on the cpolar official website).

On this page, you also need to make several information settings, which include:

  1. 隧道名称——It can be regarded as the local tunnel information annotation of cpolar, as long as it is convenient for us to distinguish;
  2. 协议——Tomcat outputs a web page, so the http protocol is selected;
  3. 本地地址——The local address is the output port number of the local website. We previously set 8081 as the output port, so we also fill in 8081 here;
  4. 域名类型——In this example, we have reserved the data tunnel of the second-level subdomain name in the cpolar cloud, so check "Second-level subdomain name" (if the reserved domain name is a custom domain name, check the custom domain name), And fill in the reserved second-level subdomain name in the "Sub Domain" column on the next line. Here we fill in "pythonweb" (if you only create a temporary data tunnel, directly check "Random Domain Name", and the cpolar client will generate the network by itself. address);
  5. 地区——Same as when setting up the cpolar cloud, we fill it in according to the actual location;

20230330132821

After completing these settings, you can click 创建the button at the bottom of the page to connect the local Python server web page with the second-level subdomain name reserved on the cpolar cloud to generate a complete intranet penetration data tunnel.

After the tunnel is created, cpolar will automatically jump to 隧道管理the - 隧道列表page, where you can see the status of the data tunnel (whether it is smooth), or control the opening, closing, or deletion of the tunnel. If you need to change the tunnel information, you can also do so through the "Edit" button here.

20230330132822

4. Public network access test

After the data tunnel is established, we can click on the left side of the cpolar client 状态- 在线隧道列表. Find the public internet address that enables access to the local python server page.

20230330132823

As long as you enter this address into the browser address, you can easily access the python server page on your local computer.

20230330132824

5 Conclusion

So far, we have successfully used python to build a simple server page, and created a data tunnel for it through cpolar that can penetrate the intranet barrier, and publish this python web page to the public Internet. In fact, as long as we know the output port number of the web page (or software), we can use cpolar to penetrate the intranet, allowing us to access the local web page (or software) on the public Internet.

Guess you like

Origin blog.csdn.net/cdtu_mid/article/details/132277260