search
HomeBackend DevelopmentPython TutorialTutorial on implementing FTP using socket

Tutorial on implementing FTP using socket

Jul 18, 2017 pm 01:48 PM
socketserveraccomplish

 
    1、用户加密认证
    2、允许同时多用户登录
    3、每个用户有自己的家目录 ,且只能访问自己的家目录
    4、对用户进行磁盘配额,每个用户的可用空间不同
    5、允许用户在ftp server上随意切换目录
    6、允许用户查看当前目录下文件
    7、允许上传和下载文件,保证文件一致性
    8、文件传输过程中显示进度条
    附加功能:支持文件的断点续传

python 3.5


  用socketserver实现FTP


  
    ConfigParser 是Python自带的模块, 用来读写配置文件,将用户信息以下边的格式存入account.py文件,读出后进行判断
[DEFAULT]

[alex]
Password = 123456Quotation = 100[jack]
Password = 123456Quotation = 100

 

    2、允许同时多用户登录
      从scokerserver 继承 socketserver.ThreadingTCPServer即可实现
    3、每个用户有自己的家目录 ,且只能访问自己的家目录
      将所有路径封装在home目录下
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

USER_HOME = '%s\home' % BASE_DIR
LOG_DIR = '%s\log' % BASE_DIR
LOG_LEVEL = 'DEBUG'

 

    4、允许用户在ftp server上随意切换目录
      用os模块改变工作目录
os.chdir(func_val)

 

    5、允许用户查看当前目录下文件
      用os模块的os.listdir,下为server端代码
    def _ls(self,*args,**kwargs):'''显示当前目录下的所有文件'''if os.getcwd() == '%s\\bin'% settings.BASE_DIR:
            user_home_dir = "%s\%s" % (settings.USER_HOME, self.user["Username"])
            self.request.send(json.dumps(os.listdir(user_home_dir)).encode())else:self.request.send(json.dumps(os.listdir()).encode())

 

    6、允许上传和下载文件,保证文件一致性
      判断上传和下载的文件在传输前后的大小,用以判断文件是否保持一致
  1 #server端  2 #  3     def _put(self,*args,**kwargs):  4         '''上传文件命令'''  5         data = args[0]  6   7         response = self.get_response()  8         if response["status_code"] == 257:  # ready to receive  9             self.request.send(b'1')  # send confirmation to server 10             user_home_dir = "%s\%s" % (settings.USER_HOME, self.user["Username"]) 11             base_filename = "%s\%s" % (user_home_dir, data.get('filename')) 12             received_size = 0 13             file_obj = open(base_filename, 'wb') 14             if data.get('md5'): 15                 md5_obj = hashlib.md5() 16                 while received_size 
get and put

 

    7、文件传输过程中显示进度条
      根据已传文件大小和总文件大小比对判断,输出符号
 1     def show_progress(self,total): 2         '''显示进度条 3            total: 文件大小''' 4         received_size = 0 5         current_percent = 0 6         while received_size  current_percent: 8                 print(">",end='',flush=True) 9                 current_percent = int((received_size / total) * 100)10             new_size = yield11             received_size += new_size
进度条

 

    附加功能:支持文件的断点续传
      将上次断点信息记录下来,也就是记录已传文件大小,再次启用时,将目标文件光标移到上次断点处,然后进行续传
 1     def _breakpoint(self,*args,**kwargs): 2         '''断点续传''' 3         data = args[0] 4         if data.get('filename') is None: 5             self.send_response(255) 6         user_home_dir = "%s\%s" % (settings.USER_HOME, self.user["Username"]) 7         file_abs_path = "%s\%s" % (user_home_dir, data.get('filename')) 8         print("file abs path", file_abs_path) 9         print(data.get('breakpoint'))10 11         if os.path.isfile(file_abs_path):12             file_obj = open(file_abs_path, 'rb')13             file_obj.seek(data.get('breakpoint'))14             file_size = os.path.getsize(file_abs_path)15             self.send_response(257, data={'file_size': file_size})16             self.request.recv(1)  # 等待客户端确认17 18             if data.get('md5'):19                 md5_obj = hashlib.md5()20                 for line in file_obj:21                     self.request.send(line)22                     md5_obj.update(line)23                 else:24                     file_obj.close()25                     md5_val = md5_obj.hexdigest()26                     self.send_response(258, {'md5': md5_val})27                     print("send file done...")28             else:29                 for line in file_obj:30                     self.request.send(line)31                 else:32                     file_obj.close()33                     print("send file done...")34         else:35             self.send_response(256)36         pass
breakpoint

   主要知识点:

    类的继承
    os模块的应用
    json模块的应用
    类方法的运用
    md5加密方法
    scoket链接
    scoketserver链接
    反射
    异常处理


小结:
          这个程序主要用socketserver实现了一台服务器链接多个客户端,并且进行信息交互,并且实现了几个简单的功能,如:get 文件下载、put 上传文件、cd 切换目录、ls 查看文件、breakpoint断点续传。

主要代码:
  1 #server端  2 #  3   4   5 import os,sys  6 import hashlib  7 import socket  8 import socketserver  9 import json 10 import configparser 11 from conf import settings 12  13  14 STATUS_CODE = { 15     250 : "Invalid cmd format,e.g:{'action':'get','filename':'test.py','size':344", 16     251 : "Invalid cmd", 17     252 : "Invalid auth data", 18     253 : "Wrong username or password..", 19     254 : "Passed authentication", 20     255 : "Filename doesn't provided", 21     256 : "File doesn't exist on server", 22     257 : "ready to send file", 23     258 : "md5 verification", 24     259 : "Directory has been switched" 25 } 26  27  28  29  30  31 class FTPHandler(socketserver.BaseRequestHandler): 32     '''定义request handler类,从BaseRequestHandler类继承''' 33     def handle(self): 34         ''' 35         获取服务器端的信息 36         如果传来cd命令,改变工作目录 37         ''' 38  39         while True: 40             self.data = self.request.recv(1024).strip() 41             if not self.data: 42                 print('Client closed..') 43                 break 44             data = json.loads(self.data.decode()) 45             if data.get('action') is not None: 46                 if hasattr(self,'_%s'%data.get('action')): 47                     func =getattr(self,'_%s'%data.get('action')) 48                     func_val = func(data) 49                     if data.get('action') == 'cd':     #cd 命令,改变工作目录 50                         os.chdir(func_val) 51                     else:pass 52                 else: 53                     print('Invalid cmd') 54                     self.send_response(251) 55             else: 56                 print('Invalid cmd format') 57                 self.send_response(250) 58  59  60     def send_response(self,status_code,data=None): 61         '''向客户端返回数据''' 62         response = {'status_code': status_code, 'status_msg':STATUS_CODE[status_code]} 63         if data: 64             response.update(data) 65         self.request.send(json.dumps(response).encode()) 66  67  68     def _auth(self,*args,**kwargs): 69         '''判断用户是否输入完整的用户名和密码 70            验证用户名和密码是否合法''' 71         data = args[0] 72         if data.get('username') is None or data.get('password') is None: 73             self.send_response(252) 74  75         user = self.authenticate(data.get('username'),data.get('password')) 76         if user is None: 77             self.send_response(253) 78         else: 79             print('passed authentication',user) 80             self.user = user 81             self.send_response(254) 82  83  84     def authenticate(self,username,password):       # 85         '''验证用户合法性,合法返回用户数据''' 86         config = configparser.ConfigParser() 87         config.read(settings.ACCOUNT_FILE) 88         if username in config.sections(): 89             _password = config[username]['Password'] 90             if _password == password: 91                 print('pass auth..',username) 92                 config[username]["Username"] = username 93                 return config[username] 94  95  96     def get_response(self): 97         '''接收客户端回复结果''' 98         data = self.request.recv(1024) 99         data = json.loads(data.decode())100         return data101 102 103     def show_progress(self,total):104         '''显示进度条105            total: 文件大小'''106         received_size = 0107         current_percent = 0108         while received_size  current_percent:110                 print(">",end='',flush=True)111                 current_percent = int((received_size / total) * 100)112             new_size = yield113             received_size += new_size114 115 116     def _put(self,*args,**kwargs):117         '''上传文件命令'''118         data = args[0]119 120         response = self.get_response()121         if response["status_code"] == 257:  # ready to receive122             self.request.send(b'1')  # send confirmation to server123             user_home_dir = "%s\%s" % (settings.USER_HOME, self.user["Username"])124             base_filename = "%s\%s" % (user_home_dir, data.get('filename'))125             received_size = 0126             file_obj = open(base_filename, 'wb')127             if data.get('md5'):128                 md5_obj = hashlib.md5()129                 while received_size 
server端
  1 #client端  2 #  3   4 import socket  5 import hashlib  6 import os,json  7 import optparse  8 import socketserver  9  10  11 STATUS_CODE = { 12     250 : "Invalid cmd format,e.g:{'action':'get','filename':'test.py','size':344", 13     251 : "Invalid cmd", 14     252 : "Invalid auth data", 15     253 : "Wrong username or password..", 16     254 : "Passed authentication", 17     255 : "Filename doesn't provided", 18     256 : "File doesn't exist on server", 19     257 : "ready to send file", 20     258 : "md5 verification", 21     259 : "Directory has been switched" 22 } 23  24  25  26  27 class FTPClient(object): 28     '''客户端''' 29     def __init__(self): 30         '''用户信息输入格式化 31             变量定义''' 32         parser = optparse.OptionParser() 33         parser.add_option('-s', '--server', dest='server', help ='ftp server ip_addr') 34         parser.add_option('-p','--port', type='int', dest='port', help='ftp server port') 35         parser.add_option('-U', '--username', dest='username', help='username') 36         parser.add_option('-P', '--Password', dest='password', help='password') 37         self.options, self.args = parser.parse_args() 38         self.verify_args(self.options,self.args) 39         self.make_connection() 40  41  42     def make_connection(self): 43         '''连接服务器''' 44         self.sock = socket.socket() 45         self.sock.connect((self.options.server,self.options.port)) 46  47  48  49     def verify_args(self,options,args): 50         '''校验参数合法性''' 51         if options.username and options.password: 52             pass 53         elif options.username is None and options.password is None: 54             pass 55         else: 56             if options.username is None or options.password is None: 57                 print('Error:username and password must be provided together..') 58         if options.server and options.port: 59             if options.port > 0 and options.port  current_percent:108                 print(">",end='',flush=True)109                 current_percent = int((received_size / total) * 100)110             new_size = yield111             received_size += new_size112 113 114     def send_response(self,status_code,data=None):115         '''向服务器端返回数据'''116         response = {'status_code': status_code, 'status_msg':STATUS_CODE[status_code]}117         if data:118             response.update(data)119         self.sock.send(json.dumps(response).encode())120 121 122     def get_response(self):123         '''得到服务器端回复结果'''124         data = self.sock.recv(1024)125         data = json.loads(data.decode())126         return data127 128 129     def _get(self,cmd_list):130         '''下载文件'''131         print("get--",cmd_list)132         if len(cmd_list) == 1:133             print("no filename follows...")134             return135         data_header = {136             'action':'get',137             'filename':cmd_list[1]138         }139         if self.__md5_required(cmd_list):140             data_header['md5'] = True141 142         self.sock.send(json.dumps(data_header).encode())143         response = self.get_response()144         print(response)145         try:146             if response["status_code"] == 257:#ready to receive147                 self.sock.send(b'1') #send confirmation to server148                 base_filename = cmd_list[1].split('/')[-1]149                 received_size = 0150                 file_obj = open(base_filename,'wb')151                 if self.__md5_required(cmd_list):152                     md5_obj = hashlib.md5()153                     progress = self.show_progress(response['file_size']) #generator154                     progress.__next__()155                     while received_size " % cmd_list[1],end='')279 280 281     def _breakpoint(self,*args,**kwargs):282         '''断点续传'''283         with open('data\\breakpoint', 'rb') as br_po:284             data_header = json.loads(br_po.read().decode())285             br_po.close()286         if data_header:287             print(data_header)288             self.sock.send(json.dumps(data_header).encode())289             response = self.get_response()290             try:291                 if response["status_code"] == 257:  # ready to receive292                     self.sock.send(b'1')  # send confirmation to server293                     base_filename = data_header['filename'].split('/')[-1]294                     received_size = data_header['breakpoint']295                     file_obj = open(base_filename, 'ab')296                     file_obj.seek(data_header['breakpoint'])297                     if self.__md5_required(data_header):298                         md5_obj = hashlib.md5()299                         progress = self.show_progress(response['file_size'])  # generator300                         progress.__next__()301                         while received_size 
client端

Create a TCP server
Idea: create a socket---bind the socket to a local address---monitor the connection---answer TCP client requests in an infinite loop (will return to the client after receiving end socket)---Accept TCP data---Send TCP data---Close sub-socket---Close server socket
from socket import *
from time import ctime

HOST = '' # Indicates that any valid address can be bound
PORT = 2157
BUFSIZ = 1024 # Keyword, size is 1k
ADR= (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM) # Create a network-based, TCP server socket
tcpSerSock.bind(ADR)
tcpSerSock.listen(5) # Only allow 5 connections at the same time

while True: # Infinite loop answer
print 'waiting for connection....'
tcpCliSock, addr = tcpSerSock.accept() # Receive and save the client socket and client address
print '...connected from:', addr

while True:
data = tcpCliSock.recv(BUFSIZ) # Read buffer data
if not data:
break
tcpCliSock.send('{0} {1}' .format(ctime(), data)) #Send information to the client
tcpCliSock.close()
tcpSerSock.close()

Create TCP client
Idea: Create a client socket---connect---send data (exit if the send is empty)---receive data from the buffer (exit if the reception fails)-- -Close client socket
from socket import *

HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while True:
data = raw_input('>')
if not data:
break
tcpCliSock.send(data)
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print data
tcpCliSock.close()

Create a UDP server
Idea: create a socket---bind the socket to a local address--receive data---send data
from socket import *
from time import ctime

HOST = '' # Indicates that any valid address can be bound
PORT = 2157
BUFSIZ = 1024 # Keyword, size is 1k
ADR= (HOST, PORT)

udpSerSock = socket(AF_INET, SOCK_DGRAM)
udpSerSock.bind(ADR)

while True:
print 'waiting for message...'
data, addr = udpSerSock. recvfrom(BUFSIZ)
udpSerSock.sendto('{0} {1}' .format(ctime(), data), addr)
print '...received from and returned to:', addr
udpSerSock.close()

Create a UDp client
Idea: Create a client socket---send data---receive data
from socket import *
from time import ctime

HOST = 'localhost' # Indicates that any valid address can be bound
PORT = 2157
BUFSIZ = 1024 # Keyword, size is 1k
ADDR= (HOST, PORT)

udpCliSock = socket(AF_INET, SOCK_DGRAM)

while True:
data = raw_input('>')
if not data:
break
udpCliSock .sendto(data, ADDR)
data, ADDR = udpCliSock.recvfrom(BUFSIZ)
if not data:
break
print data
udpCliSock.close()

The above is the detailed content of Tutorial on implementing FTP using socket. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools