Use Python to build an HTTP server and publish it to the public Internet for external network access-intranet penetration

1 Introduction

As a relatively popular programming language, Python has simple syntax and clear statements, and python has good compatibility. It can be easily connected with modules established in other programming languages ​​(such as C/C++), and python is rich and powerful. Libraries are packaged and can be easily called, so they are 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. Build a 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 a single line of commands to create an http service. But relatively, this simple http service cannot provide complex functions, and can only provide relatively simple directory services. But for those who want to learn and be familiar with python, this is another necessary step.

2.1.Python installation and setup

First, we need to install the python program locally. The Python program can be found on its official website ( www.python.org/ ) to download the corresponding operating system version. The author uses the Windows operating system, so choose the Windows version to download.

20230330132801

20230330132802

After the Python download is complete, click the installer.exe directly to install it. At the same time, it should be noted that during the installation process, it is necessary to check Add python.exe to PATHthe options.

20230330132803

20230330132804

After completing the installation of the python program, we can start to build 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 open the http service easily. Of course, to run a web page, the web page needs to have display content. Therefore, we can first create a folder for storing 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 to the hard disk where the file is stored

e:

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

Among them, 8081 is the output port of the server to be built, as long as you choose an unoccupied port. If a firewall prompt message 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 address bar of the browser on the local computer localhost:8081to open the file in the folder to be shared.

20230330132809

Being able to see these contents means that the http service of python on the local computer has been enabled, and the files in this folder can already be checked. Similarly, if we put webpage files in this folder, they can also be displayed as webpages.

At this time, for devices under the same LAN, as long as you enter the ip address of the local computer + port number (the specific format is 192.168.XXX.XXX: port number) in the browser, the python web page can 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 the official website of cpolar ( https://www.cpolar.com/ ). The author also chooses the Windows version here.

20230330132811

20230330132812

After the Cpolar software is downloaded, decompress 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 to use cpolar to create an intranet penetration data tunnel to connect the python web page of the local computer with 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 in order to obtain a long-term stable intranet penetration data tunnel.

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

20230330132817

20230330132818

On the "Reservation" page, we can see that cpolar provides data tunnel reservation items for various protocols. And the python server is the http protocol, so we find 保留二级子域名the field. Of course, if you have already purchased your own domain name from a domain name provider, you can also select the "Reserve Custom Domain Name" column.

In the "reserved second-level subdomain name" field, we need to set the information of the second-level subdomain name we intend to reserve, and the information set will also become the tunnel entrance for us to access the local python server on the public Internet (the address of the public Internet 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 the 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 keep this data tunnel. If we don't want the entrance of this tunnel anymore, we can also click the "x" on the right to delete the tunnel easily

20230330132819

3.2 Cpolar Local Settings

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

20230330132820

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

On this page, several information settings also need to be made, including:

  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 web pages, so choose http protocol;
  3. 本地地址——The local address is the output port number of the local website. We set 8081 as the output port before, so 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 the "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 just create a temporary data tunnel, directly check "random domain name", and the cpolar client will generate the network by itself address);
  5. 地区——The same as the cpolar cloud setting, we fill in the nearest location 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 webpage with the second-level subdomain 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 unblocked), or control the opening, closing or deletion of the tunnel. If you need to change the information of the tunnel, you can also use 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 a public internet address where you can access your local python server page.

20230330132823

Just enter this address into the browser address, and you can easily access the python server page on the 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 page to the public Internet. In fact, as long as we know the output port number of the webpage (or software), we can use cpolar to penetrate the intranet, allowing us to access the local webpage (or software) on the public Internet.

Guess you like

Origin blog.csdn.net/m0_73868817/article/details/132474259