Realize file sharing and port mapping between different LANs, using the HTTP service that comes with Python

1 Introduction

As the basic application of the Internet, data sharing and connection are not only widely used in business and office scenarios, but also have strong practical significance for individual users. Because of this, a lot of data sharing software has been developed, and the concept of cloud storage has been repeatedly hyped. For the author who loves tossing, it is also very interesting to use the simplest tools to find private sharing and storage solutions. Today, the author will introduce to you how to use a simple programming language such as python to build a shared file server on your computer, and turn it into a private cloud disk that can be accessed on the public Internet through the data tunnel created by cpolar .

2. Build a local file server

As a relatively simple computer language, Python is very popular all over the world, and it has become one of the compulsory courses for beginners in computer technology. Having said so much, in fact, the success of python comes from its simplicity and powerful functions. For example, now, we can use a few lines of simple code to build a python http.server file sharing server.

2.1 Python installation and setup

First, we need to download a python (you can go directly to python's official website https://www.python.org/ to download)

20230322092201

20230322092202

After the download is complete, double-click .exethe file directly to install it.

20230322092203

During the installation process, remember to add python to the environment variable ( path) option to check

20230322092204

Next, create a folder to hold the files you plan to publish. For the sake of illustration, we will name this folder asserver

20230322092205

Then run as an administrator cmdto set up python.

20230322092206

First enter in cmd python, as long as the following figure can be displayed, it means that the python installation is successful.

20230322092207

Then enter the command line to transfer to the E drive

e:

Enter the folder to be shared

cd server

Finally, enter the command to execute the port output shared command of the folder.

python -m http.server 9090  #其中端口号可以自选,但必须为未被占用的,可以选择一些偏僻的端口

After the command is entered, a firewall query may appear, we can click it 允许访问.

It should be noted that the shared service function of python is very simple and basic. Here, the shared folder is the folder where the command is currently executed, and a certain folder cannot be specified. Therefore, we need to change the drive letter and folder before executing the shared command.

20230322092208

Seeing that cmd displays the above content, you can access the local port 9090 in the browser, enter in the address bar localhost:9090, and access the file shared by python.

20230322092209

Due to the simple functionality of python, we cannot close the process of python, otherwise the file sharing port will be closed.

2.2 Installation and registration of cpolar

After completing the python folder sharing settings, we can install and register cpolar to prepare for the subsequent release of shared files. The download address of Cpolar can be found in the official website of cpolar ( https://www.cpolar.com/ ).

20230322092210

On the cpolar download page, find the corresponding version of the cpolar installer. I use the Windows operating system, so I choose the Windows version to download.

20230322092211

After the download is complete, unzip the downloaded file, and double-click .msithe file to automatically execute the cpolar installation program. We only need to go all the way Nextto complete the installation.

20230322092212

20230322092213

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.

20230322092214

20230322092215

3. Release of local file server

After completing the python setup, installing and registering cpolar, the rest of the work is simple, just use cpolar to build a data tunnel, the entrance of the data tunnel is the public Internet address, and the exit connects to the local python shared file web page.

3.1 Cpolar cloud settings

Since the need to share files may occur at any time, and I don’t want to set up a data tunnel every time I want to share files, I upgrade cpolar to the VIP version in order to keep the long-term stable data tunnel (the data created by the free version of cpolar The tunnel resets every 24 hours).

After upgrading cpolar to vip, we first log in to the cpolar cloud, find 预留the button on the left side of the customer main interface, and click to enter the reserved page.

20230322092216

On the reserved page, you can see that cpolar provides multiple protocol data tunnel reservations, and our python is the output of the web page, so find the field 保留二级子域名and set the information in this field.

20230322092217

The set information content 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 completing these settings, you can click 保留the button on the right to keep this data tunnel.

20230322092218

If this data tunnel is no longer intended to be used, you can click on the right xto delete it easily, saving valuable tunnel quota.

20230322092219

3.2 Cpolar Local Settings

After completing the reservation of the data tunnel entry in the cpolar cloud, we return to the cpolar local client and connect the data tunnel entry generated by the cpolar cloud with the local python web page.

First log in to the cpolar client, (you can enter direct access in the browser localhost:9200, or click the shortcut of the cpolar client in the start menu), 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 The temporary data tunnel that resets the address every 24 hours can be set directly 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 seen as cpolar's local tunnel information comment, as long as it is convenient for us to distinguish
  2. 协议: Since python's sharing is in the form of a web page, choosehttp协议
  3. 本地地址: The local address is the output port number of the local website. Before we set the output port of python to 9090, so it is also filled here9090
  4. 域名类型: In this example, we have reserved the data tunnel of the second-level subdomain name in the cpolar cloud, so check it 二级子域名(if the reserved domain name is a custom domain name, check the custom domain name), and Sub Domainfill in the next column Reserved second-level subdomain name, in this example it is "pythontest". If you plan to create a temporary data tunnel, you can directly check "random domain name", and the cpolar client will generate the network address by itself
  5. 地区: Same as the information reserved in the cpolar cloud, we can fill it in according to the actual use

20230322092220

After completing these settings, you can click the button below 创建to complete the construction of this data tunnel that can be stored for a long time.

After the data tunnel is created, cpolar will automatically jump to the 隧道管理-- 隧道列表page. On this page, we can manage this data tunnel, including opening, closing or deleting this tunnel, and we can also click 编辑the button to modify the information of this data tunnel.

20230322092221

And python's data tunnel entry (public Internet access address), you can find it in 状态-- 在线隧道列表.

20230322092222

4. Public network access test

If we paste this 公网地址into the browser on other devices, we can see the file sharing webpage generated by python on the local computer, and can easily view the shared files.

20230322092223

5 Conclusion

So far, we have successfully released the python file sharing webpage through cpolar intranet penetration. Although this python file sharing webpage is too simple and its functions are very simple, it can be well displayed. The output port of the webpage (or software) and the cpolar data tunnel The relationship between the port settings, that is, the output port of the web page (or software), must be consistent with the port filled in by the cpolar data tunnel, so that the public Internet address and the local web page (or software) can be accurately connected.

Guess you like

Origin blog.csdn.net/qq_53317005/article/details/132634614