每日10行代码37:ftplib的简单使用

今天整理了下以前的ftp连接代码:

#coding=utf-8
from ftplib import FTP

import sys
import io

ftp = FTP()
ftp.set_debuglevel(0)  
ftp.connect("148.28.19.1",21)
ftp.login("user","password")
ftp.encoding ='gb2312'
ftp.cwd("test\上传")   # 改变当前的工作目录

filelist=[]

ftp.retrlines('LIST',filelist.append)
for item in filelist:
	print(item)

其中关键的函数是retrlines ,其官方文档的定义是:

FTP.retrlines(cmd, callback=None)
Retrieve a file or directory listing in ASCII transfer mode. cmd should be an appropriate RETR command (see retrbinary()) or a command such as LIST or NLST (usually just the string ‘LIST’). LIST retrieves a list of files and information about those files. NLST retrieves a list of file names. The callback function is called for each line with a string argument containing the line with the trailing CRLF stripped. The default callback prints the line to sys.stdout.

其中cmd命令可以参考这个博客:
https://blog.csdn.net/weiyuefei/article/details/51758288
里面介绍的很详细

总体来说,ftplib封装程度还不够,好多操作需要理解ftp的底层原理。比如上面需要用到ftp的命令。在网上找了下,找到这个人的博客https://www.jianshu.com/p/0255e3ac6691 不过代码排版有些乱,没有测试过是否可用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值