During this period of time, I learned Python and came into contact with socket in network programming. In addition, the Linux server I was using supported Python native, so I came up with the idea of a program with file transfer function.

After all, there are a lot of frameworks for downloading in Python, but programs that quickly set up file (folder) transfer channels between hosts seem to be uncommon, because I’m new to Python, but I don’t know and I’m not surprised. In general, I feel a sense of accomplishment when I do it myself.

Project address github.com/Ccapton/pyt…

In field preview

The local host sends files to the remote server host.

Mind mapping

File transfer system

The above mind map is for reference only. After all, the expression ability is limited, and the specific functions can only be analyzed in the code.

The principle of

Socket based communication, I believe will be familiar with programming friends, and through socket to transfer files is also very common, but this is only for a single file is very easy to achieve.

What about multiple files?

I tried several times before implementing this system, but it was impractical to use a single socket channel to transfer multiple files, because when calling socket.recv(), the data format returned was raw STR, making it difficult to split the data between different files.

Because it involves receiving and sending both ends of the file data receiving and sending progress of the unity, it is necessary to use another instruction to control the transmission work is not messy, so I thought of more than open a socket as the transmission instruction channel, so that the instruction and data are separated, it is easy to control the transmission work! So there are instruction threads and data threads.

To achieve the transmission of the entire folder, the first to traverse this folder, the structure of all files in its accurate restoration; Because it is through a socket through the transmission of data, so the transmission of files can only be one after another, so that the traversal work can only wait for the completion of the previous file transmission to continue, so it is necessary to design the traversal work.

After modification, I added the body of the while loop and the callback class for external inheritance in the file finder, so that I can achieve the desired effect of file transfer through the socket in order.

FileFinder (blocking)

# Block design: To wait for the caller to do a time-consuming operation [otherwise the caller will quickly complete the file traversal task, and the caller will not achieve the intent of operating the file (folder) sequentially] class FileFinder: def __init__(self,finderCallback): Self. sum_size = 0 # caller-controlled argument, False to continue the loop, True to block the loop, If the value is False, it will work normally. If the value is True, the loop will not block when the value is False. Def onFindDir(self,dir_path): def onFindDir(self,dir_path): Def onFindFile(self,file_path,size): Def list_flie(self,root_dir): if os.path.isfile(root_dir): while self.recycle: Time. Sleep. (0.05) if the self finderCallback: self.finderCallback.onFindFile(root_dir,os.path.getsize(root_dir)) self.finderCallback.onRefresh() if not self.off: Self. recycle = True else: dirlist = os.listdir(root_dir) # path = os.path.join(root_dir, dir) if os.path.isfile(path): while self.recycle: Time. Sleep. (0.05) if the self finderCallback: self.finderCallback.onFindFile(path,os.path.getsize(path)) self.finderCallback.onRefresh() if not self.off: Self. Recycle = True else: While self.recycle: time.sleep(0.05) if self.finderCallback: self.finderCallback.onFindDir(path) self.finderCallback.onRefresh() if not self.off: Self.list_flie (path) self.list_flie(path)Copy the code

By inheriting FileFinder’s callback class FinderCallback, override its onFindDir(self,dir_path) and onFindFile(self,file_path,size) methods to send instructions to the receiver.

But the most difficult is the processing of socket instruction information in both ends of the instruction thread, which requires in-depth code to specific research, my ability is limited, I will not go into detail.

Pay attention to

To run the receiver program, an accessible address is required, that is, it is best to transfer files within the LAN, because the local IP address of the LAN can be directly accessed. If you transfer files on the public network, you must know the public IP address and internal IP address of the receiver host.

For example, now I use the host of Tencent Cloud as the receiver, the internal IP is 10.135.xxx.xxx, and the public IP is 111.120.xxx.xxx. In this host, the receiving (service) end program is run

Python3 ftserver.py -i 10.135.xxx. XXX -d /home/ubuntu/downloadsCopy the code

While running the send (client) program on your host send folder Bilibili

Python3 ftclient. Py - I 111.120. XXX. XXX -f/Users/capton/desktop/bilibiliCopy the code

Because Tencent cloud, Ali cloud and other domestic virtual host providers are using NAT address translation for cloud host address allocation, so in accordance with my above steps to run both ends of the program to connect.

The original link: www.jianshu.com/p/07ff2a7d2… Wenyuan network, only for the use of learning, such as infringement, contact deletion.

I have compiled good technical articles and lessons learned on my public account, Python Circle.

You will definitely encounter difficulties in learning Python. Don’t panic, I have a set of learning materials, including 40+ e-books, 600+ teaching videos, covering Python basics, crawlers, frameworks, data analysis, machine learning, etc. There are learning exchange group, learning progress together ~