zoukankan      html  css  js  c++  java
  • python + docker, 实现天气数据 从FTP获取以及持久化(三)-- python获取FTP数据

    前言

    经过前面两个小节的介绍,我们已经完成了MySQL数据库的搭建和数据库操作的事宜。 在本小节中,我们需要完成的任务是:使用python从FTP服务其上面获取文本文件。

    搭建测试FTP服务器

    LZ的测试环境是在 Windows2012 (实体机上的操作系统) + Ubuntu 16.04 (虚拟机)。 为了简单起见,我们就将FTP服务器搭建在 Windows 系统上面。开发和测试在 Ubuntu 系统上面。

    1. 打开FTP设置 (Controlpanel -> Turn windows features on or off -> Add roles and features -> Server roles)

     如上图所示,勾选上项目后,点击 “Next”, 系统会自动完成安装。

    2. 打开IIS, 设置FTP服务器

    2.1 新建FTP Site: Site -> Add FTP Site 填写FTP服务器的名字和物理路径(实际文件存放在硬盘上位置), 完成后点击 Next

    2.2 设置FTP地址以及SSL (LZ这里选择的是 No SSL, 当然也可以根据自己的需要设置);完成后点击 Next

     2.3 设置用户以及使用权限

    请注意: 这里的用户 SPCAdmin 是LZ登录系统的用户名。 我们也可指定其他用户来访问FTP,但是必须保证用户存在(需要在创建FTP之前先创建好)

    2.4 验证FTP服务器,打开浏览器,输入地址和端口号

    按照提示输入正确的用户名密码后,我们就可以看到我们放到FTP服务器上的文件啦

    python编程访问FTP

    我们将其封装成一个python的文件 FTPUtil.py; 整体代码如下

    # -*- coding: utf-8 -*-
    
    #from ctypes import *
    import os
    #import sys
    import ftplib
    
    
    class FTPUtil:
        ftp = ftplib.FTP()
        bIsDir = False
        path = ""
    
        def __init__(self, host, port='21'):
            self.ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
            # self.ftp.set_pasv(0)      #0主动模式 1 #被动模式
            self.ftp.connect(host, port)
    
        def Login(self, user, passwd):
            self.ftp.login(user, passwd)
            print self.ftp.welcome
    
        def DownLoadFile(self, LocalFile, RemoteFile):
            file_handler = open(LocalFile, 'wb')
            self.ftp.retrbinary("RETR %s" % (RemoteFile), file_handler.write)
            file_handler.close()
            return True
    
        def UpLoadFile(self, LocalFile, RemoteFile):
            if os.path.isfile(LocalFile) == False:
                return False
            file_handler = open(LocalFile, "rb")
            self.ftp.storbinary('STOR %s' % RemoteFile, file_handler, 4096)
            file_handler.close()
            return True
    
        def UpLoadFileTree(self, LocalDir, RemoteDir):
            if os.path.isdir(LocalDir) == False:
                return False
            print "LocalDir:", LocalDir
            LocalNames = os.listdir(LocalDir)
            print "list:", LocalNames
            print RemoteDir
            self.ftp.cwd(RemoteDir)
            for Local in LocalNames:
                src = os.path.join(LocalDir, Local)
                if os.path.isdir(src):
                    self.UpLoadFileTree(src, Local)
                else:
                    self.UpLoadFile(src, Local)
    
            self.ftp.cwd("..")
            return
    
        def DownLoadFileTree(self, LocalDir, RemoteDir):
            print "remoteDir:", RemoteDir
            if os.path.isdir(LocalDir) == False:
                os.makedirs(LocalDir)
            self.ftp.cwd(RemoteDir)
            RemoteNames = self.ftp.nlst()
            print "RemoteNames", RemoteNames
            print self.ftp.nlst("/del1")
            for file in RemoteNames:
                Local = os.path.join(LocalDir, file)
                if self.isDir(file):
                    self.DownLoadFileTree(Local, file)
                else:
                    self.DownLoadFile(Local, file)
            self.ftp.cwd("..")
            return
    
        def show(self, list):
            result = list.lower().split(" ")
            if self.path in result and "<dir>" in result:
                self.bIsDir = True
    
        def isDir(self, path):
            self.bIsDir = False
            self.path = path
            # this ues callback function ,that will change bIsDir value
            self.ftp.retrlines('LIST', self.show)
            return self.bIsDir
    
        def close(self):
            self.ftp.quit()
    

    最后,编写测试程序

    if __name__ == "__main__":
        ftp = FTPUtil('10.137.185.88')
        ftp.Login('SPCAdmin', 'Siemens@2017')
    
        ftp.DownLoadFile('AHHFCH-sun-2018042706_local.txt', 'AHHFCH-sun-2018042706.txt')
        # ftp.DownLoadFileTree('del', '/del1')  # ok
        # ftp.UpLoadFileTree('del', "/del1")
        ftp.close()
        print "ok!"

     参考

    FTP服务器搭建 : https://blog.csdn.net/exlsunshine/article/details/29181465

    Python FTP: http://www.cnblogs.com/kaituorensheng/p/4480512.html

  • 相关阅读:
    oracleDBA-D4
    oracleDBA-D3
    oracleDBA-D2
    大数据架构学习记录
    UBUNTU 安装最新版 nodejs
    datax 单条记录超过大小限制,当前限制为:67108864
    将anaconda中已存在的虚拟环境增加到jupyterlab
    jupyter 启动python3 内核 总是出现错误 ImportError: cannot import name 'create_prompt_application'
    CDH 本地hadoop支持读写 AWS S3a
    hadoop 集群集成jupyterhub 出现的问题
  • 原文地址:https://www.cnblogs.com/atuotuo/p/9213064.html
Copyright © 2011-2022 走看看