FTP file transfer implementation in python, both server-side and client-side, required
(1) Client access to the server side to have an authentication function
(2) Multiple clients can access the server side
(3) Renamed files can be re-uploaded or downloaded.
FTP (File Transfer Protocol) is one of the protocols in the TCP/IP protocol group.The FTP protocol consists of two components, one is the FTP server and the other is the FTP client. One is the FTP server and the other is the FTP client. The FTP server is used to store files and the user can use the FTP client to access the resources located on the FTP server through the FTP protocol. While developing a website, the FTP protocol is usually used to transfer web pages or programs to the web server. It works in the fourth layer of the TCP model, that is, the application layer, the use of TCP transmission rather than UDP, the customer and the server to establish a connection before going through a "three handshakes" process, to ensure that the connection between the customer and the server is reliable, and is connection-oriented, to provide a reliable guarantee for the transmission of data.
server-side
First of all, to realize the authentication of the accessing client, create a database file locally and write the client's username and password into the file. In this way, the user name and password will be matched with the one existing in the database every time it is accessed to realize the authentication function. Here the passwords are encrypted with MD5 to ensure that they are not easily disclosed.
{"username": "ahpu", "password": "96e79218965eb72c92a549dd5a330112", "limitsize": 10240000, "homepath": "D:\\FTP\\home\\ahpu"}
Login authentication function implementation
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : hgh import hashlib import os import json from conf import settings class User_auth(object): def auth(self, account_info): """ # This function is to verify the user's login information, if the login is successful, then return the user's corresponding http status code and account information, otherwise only return the http status code. :param account_info: User's account information:user ID,cryptographic :return: """ name = account_info.split(":")[0] pwd = account_info.split(":")[1] pwd = (()) # Convert the password of a username to a hash value user_db_file = + r"\%" % name # Can also be written as "\\%" or "/%" if (user_db_file): # The entered user name exists with open(user_db_file) as fr: user_db_info = (()) # or (fr) if pwd == user_db_info['password']: return "200", user_db_info # OK, client request successful else: return "403.11", None # Wrong password else: return "400", None # User name does not exist, user authentication failed def hash(self, pwd): """ Password encryption for the user :param self. :param pwd: user's password :return. """ m = hashlib.md5() (pwd) return ()
Then comes the retransmission function implementation
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : hgh import hashlib import sys class Breakpoint(object): # This module confirms whether the file uploaded or downloaded by the user exists, and if so, whether it needs to be uploaded again and again. def transfer(self, filename, has_send_size, total_size, conn): """ Perform a continuation pass :param filename. :param has_send_size: size of file already sent :param total_size: Total size of files to be transferred :param conn: interface for client-server data exchange :return. """ with open(filename, 'rb') as fr: (has_send_size) # Locate the location of the continuation of the transmission print("has_send", has_send_size, "total", total_size) m = hashlib.md5() if has_send_size == total_size: self.progress_bar(has_send_size, total_size) for line in fr: (line) (line) has_send_size += len(line) # self.progress_bar(has_send_size,total_size) return () def progress_bar(self, has_send_size, total_size): bar_width = 50 # Progress bar length process = has_send_size / total_size send_bar = int(process * bar_width + 0.5) # Length of the progress bar occupied by the sent data, rounded to the nearest whole number ("#" * send_bar + "=" * (bar_width - send_bar) + "\r") # Note: it can only be written this way to fulfill the requirement ("\r%.2f%%: %s%s" % (process * 100, "#" * send_bar, "=" * (bar_width - send_bar))) # Note: To add \r\n in pycharm # With sublime just \r otherwise line breaks ()
Server-side code
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : hgh import sys import os from core import socket_server path = (((__file__))) (path) if __name__ == "__main__": HOST, PORT = "192.168.40.1", 9901 server = socket_server.((HOST, PORT), socket_server.MyTCPServer) server.serve_forever()
client (computing)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : hgh from core import socket_client import os import sys path = ((__file__)) (path) if __name__ == "__main__": host, port = "192.168.40.1", 9901 myClient = socket_client.MySocketClient(host, port) ()
Due to space limitations, the specific server-side and client-side code is placed on github at/heguohang/FTP-python
summarize
to this article on python FTP file transfer (server-side and client-side) of the article is introduced to this, more related python ftp file transfer content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!