python读取ftp文件入库_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 *

importos#import sys

importftplibclassFTPUtil:

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)defLogin(self, user, passwd):

self.ftp.login(user, passwd)printself.ftp.welcomedefDownLoadFile(self, LocalFile, RemoteFile):

file_handler= open(LocalFile, 'wb')

self.ftp.retrbinary("RETR %s" %(RemoteFile), file_handler.write)

file_handler.close()returnTruedefUpLoadFile(self, LocalFile, RemoteFile):if os.path.isfile(LocalFile) ==False:returnFalse

file_handler= open(LocalFile, "rb")

self.ftp.storbinary('STOR %s' % RemoteFile, file_handler, 4096)

file_handler.close()returnTruedefUpLoadFileTree(self, LocalDir, RemoteDir):if os.path.isdir(LocalDir) ==False:returnFalseprint "LocalDir:", LocalDir

LocalNames=os.listdir(LocalDir)print "list:", LocalNamesprintRemoteDir

self.ftp.cwd(RemoteDir)for Local inLocalNames:

src=os.path.join(LocalDir, Local)ifos.path.isdir(src):

self.UpLoadFileTree(src, Local)else:

self.UpLoadFile(src, Local)

self.ftp.cwd("..")return

defDownLoadFileTree(self, LocalDir, RemoteDir):print "remoteDir:", RemoteDirif os.path.isdir(LocalDir) ==False:

os.makedirs(LocalDir)

self.ftp.cwd(RemoteDir)

RemoteNames=self.ftp.nlst()print "RemoteNames", RemoteNamesprint self.ftp.nlst("/del1")for file inRemoteNames:

Local=os.path.join(LocalDir, file)ifself.isDir(file):

self.DownLoadFileTree(Local, file)else:

self.DownLoadFile(Local, file)

self.ftp.cwd("..")return

defshow(self, list):

result= list.lower().split(" ")if self.path in result and "

self.bIsDir=TruedefisDir(self, path):

self.bIsDir=False

self.path=path#this ues callback function ,that will change bIsDir value

self.ftp.retrlines('LIST', self.show)returnself.bIsDirdefclose(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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值