[Python] Quickly and easily build an HTTP server and access “cpolar intranet penetration” from the public network

Article directory

  • 1 Introduction
  • 2. Build a local http server
    • 2.1.Python installation and setup
    • 2.2.Python server setup and testing
  • 3. Installation and registration of cpolar
    • 3.1 Cpolar cloud settings
    • 3.2 Cpolar Local Settings
  • 4. Public network access test
  • 5 Conclusion

Reprinted from the article of remote intranet penetration: [Python] Quickly and easily build an HTTP server and access “cpolar intranet penetration” from the public network

1. Preface

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 in A powerful library that can be easily called after packaging, 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. 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 downloaded from its official website (www.python.org/) corresponding to the 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 the Add python.exe to PATH option.

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 command prompt 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 start http service

  • 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 Allow access.

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 localhost:8081 in the address bar of the browser on the local computer to open the files 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, just enter the ip address of the local computer + port number (the specific format is 192.168.XXX.XXX:port number) in the browser, and 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.cpolar installation and registration

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 the .msi file to install it automatically. We only need to click Next 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 User Registration in the upper right corner of the cpolar homepage, fill in the necessary information on the registration page, and the registration will be completed.

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 on the left side of the “Dashboard” page, find and click the Reserved button to enter the “Reserved” “page.

20230330132817

20230330132818

On the “Reservation” page, we can see that cpolar provides data tunnel reservation items for various protocols. The python server uses the http protocol, so we find the reserve second-level subdomain name 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:

  • Region: the region where the server is located, just select the nearest one
  • Second-level domain name will eventually appear in the generated public Internet address as one of the identifiers of the network address
  • description 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 Reserve button on the right to preserve 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 Tunnel ManagementCreate Tunnel 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 Set it here directly, no need to set a blank data tunnel on the official website of cpolar).

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

  1. Tunnel name – can be seen as cpolar local tunnel information comment, as long as it is convenient for us to distinguish;
  2. protocol–tomcat outputs web pages, so choose http protocol;
  3. Local address–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. Domain name type–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 reservation is Custom domain name, then 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 are only creating a temporary data tunnel, directly check “Random domain name”, the network address is generated by the cpolar client);
  5. Region – 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 Create button at the bottom of the page to connect the local python server web page with the second-level subdomain reserved in the cpolar cloud to generate a complete intranet penetration data tunnel.

After the tunnel is created, cpolar will automatically jump to the Tunnel ManagementTunnel List page, where you can see the status of the data tunnel (whether it is unblocked), or control Opening, closing or deleting of tunnels. 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 Status on the left side of the cpolar client–Online Tunnel List. 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.