当前位置:   article > 正文

Linux之FTP文件自动上传_ftp自动上传

ftp自动上传

0 背景

在服务器上部署相关应用时,需要自动采集图片进行上传,可采用搭建 FTP 服务的方式实现

FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务。 FTP是File Transfer Protocol(文件传输协议),顾名思义,就是专门用来传输文件的协议。简单地说,支持FTP协议的服务器就是FTP服务器。

FTP支持跨平台传输,因此可将FTP服务器部署在windows、linux、unix等操作系统中,本文实现的是在linux(ubuntu)上搭建FTP服务器,其它平台的可查找相关资料。

1 FTP 服务端

安装方法如下

sudo apt-get install vsftpd

安装好之后修改配置文件

sudo vim /etc/vsftpd.conf

总共有以下几处修改

  1. write_enable=YES                //使FTP服务器可写
  2. pam_service_name=ftp            //此处ubuntu的系统需要改为ftp
  3. utf8_filesystem=YES             //防止中文乱码

配置完后重启服务

service vsftpd restart

至此,FTP服务器搭建好了,我们可以在电脑文件系统上输入ftp://[服务器IP]来访问,然后输入用户名密码即可连接

2 FTP 客户端

下边的代码实现的是自动监测某个文件夹,当有新图片产生时,自动上传到服务器上

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. from ftplib import FTP
  4. import os
  5. import sys
  6. import time
  7. import socket
  8. import inotify.adapters
  9. class MyFTP:
  10. """
  11. ftp自动下载、自动上传脚本,可以递归目录操作
  12. """
  13. def __init__(self, host, port=21):
  14. """ 初始化 FTP 客户端
  15. 参数:
  16. host:ip地址
  17. port:端口号
  18. """
  19. # print("__init__()---> host = %s ,port = %s" % (host, port))
  20. self.host = host
  21. self.port = port
  22. self.ftp = FTP()
  23. # 重新设置下编码方式
  24. self.ftp.encoding = 'gbk'
  25. self.log_file = open("log.txt", "a")
  26. self.file_list = []
  27. def login(self, username, password):
  28. """ 初始化 FTP 客户端
  29. 参数:
  30. username: 用户名
  31. password: 密码
  32. """
  33. try:
  34. timeout = 60
  35. socket.setdefaulttimeout(timeout)
  36. # 0主动模式 1 #被动模式
  37. self.ftp.set_pasv(True)
  38. # 打开调试级别2,显示详细信息
  39. # self.ftp.set_debuglevel(2)
  40. self.debug_print('开始尝试连接到 %s' % self.host)
  41. self.ftp.connect(self.host, self.port)
  42. self.debug_print('成功连接到 %s' % self.host)
  43. self.debug_print('开始尝试登录到 %s' % self.host)
  44. self.ftp.login(username, password)
  45. self.debug_print('成功登录到 %s' % self.host)
  46. self.debug_print(self.ftp.welcome)
  47. except Exception as err:
  48. self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
  49. pass
  50. def is_same_size(self, local_file, remote_file):
  51. """判断远程文件和本地文件大小是否一致
  52. 参数:
  53. local_file: 本地文件
  54. remote_file: 远程文件
  55. """
  56. try:
  57. remote_file_size = self.ftp.size(remote_file)
  58. except Exception as err:
  59. # self.debug_print("is_same_size() 错误描述为:%s" % err)
  60. remote_file_size = -1
  61. try:
  62. local_file_size = os.path.getsize(local_file)
  63. except Exception as err:
  64. # self.debug_print("is_same_size() 错误描述为:%s" % err)
  65. local_file_size = -1
  66. self.debug_print('local_file_size:%d , remote_file_size:%d' % (local_file_size, remote_file_size))
  67. if remote_file_size == local_file_size:
  68. return 1
  69. else:
  70. return 0
  71. def download_file(self, local_file, remote_file):
  72. """从ftp下载文件
  73. 参数:
  74. local_file: 本地文件
  75. remote_file: 远程文件
  76. """
  77. self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))
  78. if self.is_same_size(local_file, remote_file):
  79. self.debug_print('%s 文件大小相同,无需下载' % local_file)
  80. return
  81. else:
  82. try:
  83. self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
  84. buf_size = 1024
  85. file_handler = open(local_file, 'wb')
  86. self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
  87. file_handler.close()
  88. except Exception as err:
  89. self.debug_print('下载文件出错,出现异常:%s ' % err)
  90. return
  91. def download_file_tree(self, local_path, remote_path):
  92. """从远程目录下载多个文件到本地目录
  93. 参数:
  94. local_path: 本地路径
  95. remote_path: 远程路径
  96. """
  97. print("download_file_tree()---> local_path = %s ,remote_path = %s" % (local_path, remote_path))
  98. try:
  99. self.ftp.cwd(remote_path)
  100. except Exception as err:
  101. self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
  102. return
  103. if not os.path.isdir(local_path):
  104. self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
  105. os.makedirs(local_path)
  106. self.debug_print('切换至目录: %s' % self.ftp.pwd())
  107. self.file_list = []
  108. # 方法回调
  109. self.ftp.dir(self.get_file_list)
  110. remote_names = self.file_list
  111. self.debug_print('远程目录 列表: %s' % remote_names)
  112. for item in remote_names:
  113. file_type = item[0]
  114. file_name = item[1]
  115. local = os.path.join(local_path, file_name)
  116. if file_type == 'd':
  117. print("download_file_tree()---> 下载目录: %s" % file_name)
  118. self.download_file_tree(local, file_name)
  119. elif file_type == '-':
  120. print("download_file()---> 下载文件: %s" % file_name)
  121. self.download_file(local, file_name)
  122. self.ftp.cwd("..")
  123. self.debug_print('返回上层目录 %s' % self.ftp.pwd())
  124. return True
  125. def upload_file(self, local_file, remote_file):
  126. """从本地上传文件到ftp
  127. 参数:
  128. local_path: 本地文件
  129. remote_path: 远程文件
  130. """
  131. if not os.path.isfile(local_file):
  132. self.debug_print('%s 不存在' % local_file)
  133. return
  134. if self.is_same_size(local_file, remote_file):
  135. self.debug_print('跳过相等的文件: %s' % local_file)
  136. return
  137. buf_size = 1024
  138. file_handler = open(local_file, 'rb')
  139. self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
  140. file_handler.close()
  141. self.debug_print('上传: %s' % local_file + "成功!")
  142. def upload_file_tree(self, local_path, remote_path):
  143. """从本地上传目录下多个文件到ftp
  144. 参数:
  145. local_path: 本地路径
  146. remote_path: 远程路径
  147. """
  148. if not os.path.isdir(local_path):
  149. self.debug_print('本地目录 %s 不存在' % local_path)
  150. return
  151. self.ftp.cwd(remote_path)
  152. self.debug_print('切换至远程目录: %s' % self.ftp.pwd())
  153. local_name_list = os.listdir(local_path)
  154. for local_name in local_name_list:
  155. src = os.path.join(local_path, local_name)
  156. if os.path.isdir(src):
  157. try:
  158. self.ftp.mkd(local_name)
  159. except Exception as err:
  160. self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (local_name, err))
  161. self.debug_print("upload_file_tree()---> 上传目录: %s" % local_name)
  162. self.upload_file_tree(src, local_name)
  163. else:
  164. self.debug_print("upload_file_tree()---> 上传文件: %s" % local_name)
  165. self.upload_file(src, local_name)
  166. self.ftp.cwd("..")
  167. def close(self):
  168. """ 退出ftp
  169. """
  170. self.debug_print("close()---> FTP退出")
  171. self.ftp.quit()
  172. self.log_file.close()
  173. def debug_print(self, s):
  174. """ 打印日志
  175. """
  176. self.write_log(s)
  177. def deal_error(self, e):
  178. """ 处理错误异常
  179. 参数:
  180. e:异常
  181. """
  182. log_str = '发生错误: %s' % e
  183. self.write_log(log_str)
  184. sys.exit()
  185. def write_log(self, log_str):
  186. """ 记录日志
  187. 参数:
  188. log_str:日志
  189. """
  190. time_now = time.localtime()
  191. date_now = time.strftime('%Y-%m-%d', time_now)
  192. format_log_str = "%s ---> %s \n " % (date_now, log_str)
  193. print(format_log_str)
  194. self.log_file.write(format_log_str)
  195. def get_file_list(self, line):
  196. """ 获取文件列表
  197. 参数:
  198. line:
  199. """
  200. file_arr = self.get_file_name(line)
  201. # 去除 . 和 ..
  202. if file_arr[1] not in ['.', '..']:
  203. self.file_list.append(file_arr)
  204. def get_file_name(self, line):
  205. """ 获取文件名
  206. 参数:
  207. line:
  208. """
  209. pos = line.rfind(':')
  210. while (line[pos] != ' '):
  211. pos += 1
  212. while (line[pos] == ' '):
  213. pos += 1
  214. file_arr = [line[0], line[pos:]]
  215. return file_arr
  216. if __name__ == "__main__":
  217. host_address = "10.1.*"
  218. username = "**"
  219. password = "**"
  220. my_ftp = MyFTP(host_address)
  221. my_ftp.login(username, password)
  222. i = inotify.adapters.Inotify()
  223. i.add_watch("/media/nvidia/jiaxun/20220216")
  224. for event in i.event_gen(yield_nones=False):
  225. (_, type_names, path, filename) = event
  226. if 'IN_CLOSE_WRITE' in type_names and "jpg" in filename:
  227. print("-----------------------------------")
  228. print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format(path, filename, type_names))
  229. # upload_file("video", path, filename)
  230. print("-----------------------------------")
  231. # 下载单个文件
  232. # my_ftp.download_file("G:/ftp_test/XTCLauncher.apk", "/App/AutoUpload/ouyangpeng/I12/Release/XTCLauncher.apk")
  233. # 下载目录
  234. # my_ftp.download_file_tree("G:/ftp_test/", "App/AutoUpload/ouyangpeng/I12/")
  235. # 上传单个文件
  236. my_ftp.upload_file(filename, "/home/lthpc/workspace_zong/ftp_folder/" + filename)
  237. # my_ftp.upload_file("G:/ftp_test/Python编程快速上手__让繁琐工作自动化.pdf", "/App/AutoUpload/ouyangpeng/I12/Release/Python编程快速上手__让繁琐工作自动化.pdf")
  238. # 上传目录
  239. # my_ftp.upload_file_tree("G:/ftp_test/", "/App/AutoUpload/ouyangpeng/I12/")
  240. my_ftp.close()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/404434
推荐阅读
相关标签
  

闽ICP备14008679号