栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

ftp服务器搭建

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ftp服务器搭建

ftp服务器搭建 服务端

注:ip=172.29.156.212

    安装

    yum install -y vsftpd
    systemctl start vsftpd
    systemctl status vsftpd
    

    修改配置文件
    --------------------------------匿名用户登录-------------------------

    vim /etc/vsftpd/vsftpd.conf
    anonymous_enable=YES
    anon_umask=022
    anon_upload_enable=YES
    anon_mkdir_write_enable=YES
    anon_other_write_enable=YES
    local_enable=YES
    write_enable=YES
    local_umask=022
    dirmessage_enable=YES
    xferlog_enable=YES
    connect_from_port_20=YES
    xferlog_std_format=YES
    listen=NO
    listen_ipv6=YES
    pam_service_name=vsftpd
    userlist_enable=YES
    

    ----------------------本地用户登录验证------------------

    anonymous_enable=NO
    local_enable=YES
    write_enable=YES
    local_umask=022
    dirmessage_enable=YES
    xferlog_enable=YES
    connect_from_port_20=YES
    xferlog_std_format=YES
    listen=NO
    listen_ipv6=YES
    pam_service_name=vsftpd
    userlist_enable=YES
    

    创建用户设置密码

    adduser -s /bin/false -d /var/ftp/ftp1 ftp1
    passwd ftp1
    adduser -s /bin/false -d /var/ftp/ftp2 ftp2
    passwd ftp2
    # 设置的密码为Kylin123,不要使用root用户权限太大
    # 也可以直接创建用户useradd ftp3,客户端显示的时用户的目录,如/home/ftp3
    chmod 777 /var/ftp/ftp1
    

    登录的用户不可以在这两个文件中记录

    /etc/vsftpd/ftpusers  
    /etc/vsftpd/user_list
    

    关闭防火墙并重启

    #关闭防火墙
    systemctl stop firewalld.service
    #永久禁止防火墙
    systemctl disable firewalld.service
    #重启
    systemctl restart vsftpd
    systemctl enable vsftpd
    #查看状态
    systemctl status vsftpd
    
    #----------
    setsebool -P ftpd_full_access=on
    
    或:关闭SELinux
    # vi /etc/selinux/config
    将 SELINUX=XXX -->XXX 代表级别
    改为
    SELINUX=disabled
    然后重启系统,或者执行命令:setenforce 0
    #----------------
    
客户端 虚拟机实现

    下载ftp

    yum install -y ftp
    

    登录ftp服务器
    ---------------------匿名用户登录---------------------------

    [root@localhost x86_64]# ftp 172.29.156.212
    Connected to 172.29.156.212 (172.29.156.212).
    220 (vsFTPd 3.0.3)
    Name (172.29.156.212:root): anonymous
    331 Please specify the password.
    Password:此处直接回车即可
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> ls
    227 Entering Passive Mode (172,29,156,212,117,161).
    150 Here comes the directory listing.
    drwx------    3 1000     1000           78 Oct 15 08:21 ftp1
    drwx------    3 1001     1001           78 Oct 15 08:22 ftp2
    drwxr-xr-x    3 0        0              18 Oct 15 09:13 pub
    
    ftp> cd pub
    250 Directory successfully changed.
    ftp> ls
    227 Entering Passive Mode (172,29,156,212,75,157).
    150 Here comes the directory listing.
    226 Directory send OK.
    ftp> mkdir wqz
    550 Create directory operation failed.
    #如果不能创建文件,需要去服务器上修改权限chmod 777 pub
    ftp> mkdir wqz
    257 "/pub/wqz" created
    ftp> ls
    227 Entering Passive Mode (172,29,156,212,52,89).
    150 Here comes the directory listing.
    drwxr-xr-x    2 14       50              6 Mar 08 01:45 wqz
    226 Directory send OK.
    
    

    ----------------------本地用户登录验证------------------

    [root@localhost x86_64]# ftp://172.29.156.212/
    -bash: ftp://172.29.156.212/: 没有那个文件或目录
    [root@localhost x86_64]# ftp 172.29.156.212
    Connected to 172.29.156.212 (172.29.156.212).
    220 (vsFTPd 3.0.3)
    Name (172.29.156.212:root): ftp2
    331 Please specify the password.
    Password:密码是上面设置的密码
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> ls
    227 Entering Passive Mode (172,29,156,212,214,232).
    150 Here comes the directory listing.
    226 Directory send OK.
    ftp> mkdir files
    257 "/var/ftp/ftp2/files" created
    ftp> ls
    227 Entering Passive Mode (172,29,156,212,76,9).
    150 Here comes the directory listing.
    drwxr-xr-x    2 1001     1001            6 Oct 15 10:30 files
    226 Directory send OK.
    
    

​ 或者使用本地打开我的电脑路径下输入IP,账号密码登录

ftp://172.29.156.212

python方式实现 简单版
# coding: utf-8

import os
from ftplib import FTP

def ftp_connect(host, username, password):
    ftp = FTP()
    # ftp.set_debuglevel(2)
    ftp.connect(host, 21)
    ftp.login(username, password)
    return ftp

"""
从ftp服务器下载文件
"""
def download_file(ftp, remotepath, localpath):
    bufsize = 1024  #一次性缓存为多大
    fp = open(localpath, 'wb')
    ftp.retrbinary('RETR ' + remotepath, fp.write, bufsize)
    ftp.set_debuglevel(0)
    fp.close()

"""
从本地上传文件到ftp
"""
def upload_file(ftp, remotepath, localpath):
    bufsize = 1024
    fp = open(localpath, 'rb')

    ftp.storbinary('STOR ' + remotepath, fp, bufsize)
    ftp.set_debuglevel(0)
    fp.close()


if __name__ == "__main__":
    # 会发现iso在windows和linux的大小不一致,是因为windows和linux计算方式不一致,把linux上的拉的windows上就会一致了。
    ftp = ftp_connect("172.29.156.212", "ftp1", "Kylin123")
    #下载功能实现
    # download_file(ftp, r"Kylin-Server-10-amd64-2-Release-Build01-20201214.iso", r"C:UsersLenovoPycharmProjectspythonProjectKylin-Server-10-amd64-2-Release-Build01-20201214.iso")

    #上传功能实现
    upload_file(ftp, r"test/my-iso.py", r"C:UsersLenovoPycharmProjectspythonProjectpungi-isomy-iso.py")

    ftp.quit()
复杂版
#!/usr/bin/python
# -*- coding: UTF-8 -*-


from ftplib import FTP
import os
import sys
import time
import socket


class MyFTP:
    """
        ftp自动下载、自动上传脚本,可以递归目录操作
        作者:欧阳鹏
        博客地址:http://blog.csdn.net/ouyang_peng/article/details/79271113
    """

    def __init__(self, host, port=21):
        """ 初始化 FTP 客户端
        参数:
                 host:ip地址

                 port:端口号
        """
        # print("__init__()---> host = %s ,port = %s" % (host, port))

        self.host = host
        self.port = port
        self.ftp = FTP()
        # 重新设置下编码方式
        self.ftp.encoding = 'gbk'
        self.log_file = open("log.txt", "a")
        self.file_list = []

    def login(self, username, password):
        """ 初始化 FTP 客户端
            参数:
                  username: 用户名

                 password: 密码
            """
        try:
            timeout = 60
            socket.setdefaulttimeout(timeout)
            # 0主动模式 1 #被动模式
            self.ftp.set_pasv(True)
            # 打开调试级别2,显示详细信息
            # self.ftp.set_debuglevel(2)

            self.debug_print('开始尝试连接到 %s' % self.host)
            self.ftp.connect(self.host, self.port)
            self.debug_print('成功连接到 %s' % self.host)

            self.debug_print('开始尝试登录到 %s' % self.host)
            self.ftp.login(username, password)
            self.debug_print('成功登录到 %s' % self.host)

            self.debug_print(self.ftp.welcome)
        except Exception as err:
            self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
            pass

    def is_same_size(self, local_file, remote_file):
        """判断远程文件和本地文件大小是否一致

           参数:
             local_file: 本地文件

             remote_file: 远程文件
        """
        try:
            remote_file_size = self.ftp.size(remote_file)
        except Exception as err:
            # self.debug_print("is_same_size() 错误描述为:%s" % err)
            remote_file_size = -1

        try:
            local_file_size = os.path.getsize(local_file)
        except Exception as err:
            # self.debug_print("is_same_size() 错误描述为:%s" % err)
            local_file_size = -1

        self.debug_print('local_file_size:%d  , remote_file_size:%d' % (local_file_size, remote_file_size))
        if remote_file_size == local_file_size:
            return 1
        else:
            return 0

    def download_file(self, local_file, remote_file):
        """从ftp下载文件
            参数:
                local_file: 本地文件

                remote_file: 远程文件
        """
        self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))

        if self.is_same_size(local_file, remote_file):
            self.debug_print('%s 文件大小相同,无需下载' % local_file)
            return
        else:
            try:
                self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
                buf_size = 1024
                file_handler = open(local_file, 'wb')
                self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
                file_handler.close()
            except Exception as err:
                self.debug_print('下载文件出错,出现异常:%s ' % err)
                return

    def download_file_tree(self, local_path, remote_path):
        """从远程目录下载多个文件到本地目录
                       参数:
                         local_path: 本地路径

                         remote_path: 远程路径
                """
        print("download_file_tree()--->  local_path = %s ,remote_path = %s" % (local_path, remote_path))
        try:
            self.ftp.cwd(remote_path)
        except Exception as err:
            self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
            return

        if not os.path.isdir(local_path):
            self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
            os.makedirs(local_path)

        self.debug_print('切换至目录: %s' % self.ftp.pwd())

        self.file_list = []
        # 方法回调
        self.ftp.dir(self.get_file_list)

        remote_names = self.file_list
        self.debug_print('远程目录 列表: %s' % remote_names)
        for item in remote_names:
            file_type = item[0]
            file_name = item[1]
            local = os.path.join(local_path, file_name)
            if file_type == 'd':
                print("download_file_tree()---> 下载目录: %s" % file_name)
                self.download_file_tree(local, file_name)
            elif file_type == '-':
                print("download_file()---> 下载文件: %s" % file_name)
                self.download_file(local, file_name)
            self.ftp.cwd("..")
            self.debug_print('返回上层目录 %s' % self.ftp.pwd())
        return True

    def upload_file(self, local_file, remote_file):
        """从本地上传文件到ftp

           参数:
             local_path: 本地文件

             remote_path: 远程文件
        """
        if not os.path.isfile(local_file):
            self.debug_print('%s 不存在' % local_file)
            return

        if self.is_same_size(local_file, remote_file):
            self.debug_print('跳过相等的文件: %s' % local_file)
            return

        buf_size = 1024
        file_handler = open(local_file, 'rb')
        self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
        file_handler.close()
        self.debug_print('上传: %s' % local_file + "成功!")

    def upload_file_tree(self, local_path, remote_path):
        """从本地上传目录下多个文件到ftp
           参数:

             local_path: 本地路径

             remote_path: 远程路径
        """
        if not os.path.isdir(local_path):
            self.debug_print('本地目录 %s 不存在' % local_path)
            return

        self.ftp.cwd(remote_path)
        self.debug_print('切换至远程目录: %s' % self.ftp.pwd())

        local_name_list = os.listdir(local_path)
        for local_name in local_name_list:
            src = os.path.join(local_path, local_name)
            if os.path.isdir(src):
                try:
                    self.ftp.mkd(local_name)
                except Exception as err:
                    self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (local_name, err))
                self.debug_print("upload_file_tree()---> 上传目录: %s" % local_name)
                self.upload_file_tree(src, local_name)
            else:
                self.debug_print("upload_file_tree()---> 上传文件: %s" % local_name)
                self.upload_file(src, local_name)
        self.ftp.cwd("..")

    def close(self):
        """ 退出ftp
        """
        self.debug_print("close()---> FTP退出")
        self.ftp.quit()
        self.log_file.close()

    def debug_print(self, s):
        """ 打印日志
        """
        self.write_log(s)

    def deal_error(self, e):
        """ 处理错误异常
            参数:
                e:异常
        """
        log_str = '发生错误: %s' % e
        self.write_log(log_str)
        sys.exit()

    def write_log(self, log_str):
        """ 记录日志
            参数:
                log_str:日志
        """
        time_now = time.localtime()
        date_now = time.strftime('%Y-%m-%d', time_now)
        format_log_str = "%s ---> %s n " % (date_now, log_str)
        print(format_log_str)
        self.log_file.write(format_log_str)

    def get_file_list(self, line):
        """ 获取文件列表
            参数:
                line:
        """
        file_arr = self.get_file_name(line)
        # 去除  . 和  ..
        if file_arr[1] not in ['.', '..']:
            self.file_list.append(file_arr)

    def get_file_name(self, line):
        """ 获取文件名
            参数:
                line:
        """
        pos = line.rfind(':')
        while (line[pos] != ' '):
            pos += 1
        while (line[pos] == ' '):
            pos += 1
        file_arr = [line[0], line[pos:]]
        return file_arr


if __name__ == "__main__":
    my_ftp = MyFTP("172.29.156.212")
    my_ftp.login("ftp1", "Kylin123")

    # 下载单个文件,在ftp服务器ftp1路径下有ftp-test-1.py文件,如果传的是文件夹名,则为无效下载。
    # my_ftp.download_file("./ftp-test-1.py", "ftp-test-1.py")

    # 下载目录, 在ftp服务器ftp1路径下有remote_file文件夹
    # my_ftp.download_file_tree("./remote_file", "remote_file")

    # 上传单个文件
    # my_ftp.upload_file("C:/Users/Lenovo/PycharmProjects/pythonProject/pungi-iso/my-iso.py", "my-iso.py")

    # 上传目录,注意ftp服务器上要有一个test文件,并且创建用户是上面的ftp用户。
    my_ftp.upload_file_tree("C:/Users/Lenovo/PycharmProjects/pythonProject/", "test")

    my_ftp.close()

Kylin调整版
#!/usr/bin/python
# -*- coding: UTF-8 -*-


from ftplib import FTP
import os
import sys
import time
import socket


class MyFTP:
    """
        ftp自动下载、自动上传脚本,可以递归目录操作
        作者:欧阳鹏
        博客地址:http://blog.csdn.net/ouyang_peng/article/details/79271113
    """

    def __init__(self, host, port=21):
        """ 初始化 FTP 客户端
        参数:
                 host:ip地址

                 port:端口号
        """
        # print("__init__()---> host = %s ,port = %s" % (host, port))

        self.host = host
        self.port = port
        self.ftp = FTP()
        # 重新设置下编码方式
        self.ftp.encoding = 'gbk'
        self.log_file = open("log.txt", "a")
        self.file_list = []

    def login(self, username, password):
        """ 初始化 FTP 客户端
            参数:
                  username: 用户名

                 password: 密码
            """
        try:
            timeout = 60
            socket.setdefaulttimeout(timeout)
            # 0主动模式 1 #被动模式
            self.ftp.set_pasv(True)
            # 打开调试级别2,显示详细信息
            # self.ftp.set_debuglevel(2)

            self.debug_print('开始尝试连接到 %s' % self.host)
            self.ftp.connect(self.host, self.port)
            self.debug_print('成功连接到 %s' % self.host)

            self.debug_print('开始尝试登录到 %s' % self.host)
            self.ftp.login(username, password)
            self.debug_print('成功登录到 %s' % self.host)

            self.debug_print(self.ftp.welcome)
        except Exception as err:
            self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
            pass

    def is_same_size(self, local_file, remote_file):
        """判断远程文件和本地文件大小是否一致

           参数:
             local_file: 本地文件
             remote_file: 远程文件
        """
        try:
            remote_file_size = self.ftp.size(remote_file)
        except Exception as err:
            # self.debug_print("is_same_size() 错误描述为:%s" % err)
            remote_file_size = -1

        try:
            local_file_size = os.path.getsize(local_file)
        except Exception as err:
            # self.debug_print("is_same_size() 错误描述为:%s" % err)
            local_file_size = -1

        self.debug_print('local_file_size:%d  , remote_file_size:%d' % (local_file_size, remote_file_size))
        if remote_file_size == local_file_size:
            return 1
        else:
            return 0

    def upload_file(self, local_file, remote_file):
        """从本地上传文件到ftp

           参数:
             local_path: 本地文件

             remote_path: 远程文件
        """
        if not os.path.isfile(local_file):
            self.debug_print('%s 不存在' % local_file)
            return

        if self.is_same_size(local_file, remote_file):
            self.debug_print('跳过相等的文件: %s' % local_file)
            return

        buf_size = 1024
        file_handler = open(local_file, 'rb')
        self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
        file_handler.close()
        self.debug_print('上传: %s' % local_file + "成功!")

    def upload_file_tree(self, local_path, remote_path):
        """从本地上传目录下多个文件到ftp
           参数:

             local_path: 本地路径

             remote_path: 远程路径
        """
        if not os.path.isdir(local_path):
            self.debug_print('本地目录 %s 不存在' % local_path)
            return
        try:
            self.ftp.mkd(remote_path)
        except Exception as err:
            self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (remote_path, err))

        self.debug_print('创建远程目录: %s' % remote_path)
        self.ftp.cwd(remote_path)
        self.debug_print('切换至远程目录: %s' % self.ftp.pwd())
        local_name_list = os.listdir(local_path)
        for local_name in local_name_list:
            src = os.path.join(local_path, local_name)
            if os.path.isdir(src):
                try:
                    self.ftp.mkd(local_name)
                except Exception as err:
                    self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (local_name, err))
                self.debug_print("upload_file_tree()---> 上传目录: %s" % local_name)
                self.upload_file_tree(src, local_name)
            else:
                self.debug_print("upload_file_tree()---> 上传文件: %s" % local_name)
                self.upload_file(src, local_name)
        self.ftp.cwd("..")

    def close(self):
        """ 退出ftp
        """
        self.debug_print("close()---> FTP退出")
        self.ftp.quit()
        self.log_file.close()

    def debug_print(self, s):
        """ 打印日志
        """
        self.write_log(s)

    def deal_error(self, e):
        """ 处理错误异常
            参数:
                e:异常
        """
        log_str = '发生错误: %s' % e
        self.write_log(log_str)
        sys.exit()

    def write_log(self, log_str):
        """ 记录日志
            参数:
                log_str:日志
        """
        time_now = time.localtime()
        date_now = time.strftime('%Y-%m-%d', time_now)
        format_log_str = "%s ---> %s n " % (date_now, log_str)
        print(format_log_str)
        self.log_file.write(format_log_str)


if __name__ == "__main__":
    my_ftp = MyFTP("172.29.156.212")
    my_ftp.login("ftp1", "Kylin123")
    local_path = "C:/Users/Lenovo/PycharmProjects/pythonProject/pungi-iso/remote_file/1"  # 本地存放iso路径目录
    remote_path = "ftp-test"  # 要在ftp服务器上创建的目录
    my_ftp.upload_file_tree(local_path, "1")

    my_ftp.close()


_str)
sys.exit()

def write_log(self, log_str):
    """ 记录日志
        参数:
            log_str:日志
    """
    time_now = time.localtime()
    date_now = time.strftime('%Y-%m-%d', time_now)
    format_log_str = "%s ---> %s n " % (date_now, log_str)
    print(format_log_str)
    self.log_file.write(format_log_str)

if name == “main”:
my_ftp = MyFTP(“172.29.156.212”)
my_ftp.login(“ftp1”, “Kylin123”)
local_path = “C:/Users/Lenovo/PycharmProjects/pythonProject/pungi-iso/remote_file/1” # 本地存放iso路径目录
remote_path = “ftp-test” # 要在ftp服务器上创建的目录
my_ftp.upload_file_tree(local_path, “1”)

my_ftp.close()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/757567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号