banner
肥皂的小屋

肥皂的小屋

github
steam
bilibili
douban
tg_channel

Python - Implementing Basic FTP Functionality Based on Socket

Cause#

Originating from the computer network course design at the end of the semester, the original topic was to implement FTP protocol-related functions based on DELPHI.

Since I could choose any language, I decided to use the world's best Python (no rebuttals accepted) #(blushing)

This article will reproduce a communication software based on socket communication according to the reference article.

** Since the socket communication part is not too difficult, this article will not explain too much **

Reference article:

The Simplest Socket Communication#

server side code is as follows:

client side:

The demonstration effect is as follows:

image

SocketServer Multithreaded Version#

When we start 2 clients simultaneously, we find that only one client can continuously communicate with the server, while the other client remains in a suspended state.

When the communicable client disconnects, the second client can then communicate with the server.

To allow the server to communicate with multiple clients simultaneously, we call a module called SocketServer.

server side code:

client code:

The demonstration effect is as follows:

image

Simulating FTP Server Implementation#

server side code is as follows:

client side:

The effect is as follows:

image

At this point, we have a general understanding of the socket part, and next is to design the GUI and simultaneously design the logic.

Course Design#

Since using the socketserver library requires the use of the server.serve_forever() statement to keep the server running,

And in the GUI operation, it also requires the use of sys_exit(app.exec_()) statement to keep the interface running.

Both of these statements are similar to While True, which keeps running in an internal loop, so whichever loop is used first,

It will continue to run in that loop and cannot execute the subsequent code.

The direct result of this defect is that the server and GUI cannot run simultaneously, so in the end, I gave up the GUI interface for the server side.

The tutorial for PyQt5 has already been introduced in the UI design part of the article "Building a Local IP Proxy Pool (Completed)".

Here we will supplement some additional information.

First, here is a UI design diagram:

image

As I mentioned in the article about building a local proxy pool, it is strongly recommended to wrap each part with a Frame framework.

Of course, for alignment purposes, I used two Frames for the top login area.

After dividing the areas and placing the components, I modified the commonly used names for the buttons, and then followed the "UI and Code Logic Separation" operation introduced in the proxy pool article to start writing code.

Below are a few small issues encountered while using PyQt5.

Progress Bar#

This was my first time encountering a progress bar, and I thought the effect was okay, but I couldn't find many settings, only one:

setRange to set the range and setValue, but these two are enough, my usage is as follows:

The socket transfers files locally super fast, so the effect is not noticeable; you can check the speed during the later demonstration.

Missing Qt5Core.dll#

I encountered this issue during packaging, in a brand new Python installation environment.

After sequentially pip installing pipreqs, PyQt5, pyinstaller and other libraries,

When packaging, it said that the Qt5Core.dll file was missing, and it couldn't be found in the system files.

So I found a download provided by someone online, the safety is unknown, but it works for me.

Click here to download, after downloading, place it in a location in the system environment variable, such as:

That will work.


Update: When I downloaded from csdn, this file didn't require points, but now it does; I will share the one I downloaded here.

Icon Not Displaying After Packaging#

I encountered this issue when I was writing the local proxy pool, but I forgot to mention it, so here it is.

  1. Create a qrc file and write the following content (note that the code format will throw an error if not correct):
  1. Generate a py file, this py file saves the image as binary:
  1. Import the module and set the icon:

Just add ; before the original icon name.

Salted Hash Library#

To add a gimmick for the teacher's acceptance, the client side uses encrypted hash for encrypted transmission.

The server side checks if the username is in the database after receiving the username and hash, then uses the decryption algorithm to compare the transmitted hash with the plaintext password in the database.

Reference article: Using Salted Hash Functions to Encrypt Passwords in Python

Installation: pip install werkzeug

The client side uses the generate_password_hash function for encryption:

Generally, the latter two parameters can be omitted, just use it directly.

The server side uses check_password_hash for decryption:

Final Effect Demonstration#

  1. Login and Logout

image

  1. Directory Operations

image

  1. Download

image

  1. Upload

image

  1. Delete

image

The article ends here.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.