FTP简单示例

1、ftp客户端示例代码

1.1、简单使用

示例代码如下(ftpClientReadTest1.py):

#! /usr/bin/env python
#-*- coding:utf-8 -*-

import ftplib

ftp = ftplib.FTP('127.0.0.1')
#ftp.login("UID", "PSW")
ftp.login()  # user anonymous, passwd [email protected]
ftp.retrlines('LIST')

该代码的github地址: https://github.com/mike-zhang/pyExamples/blob/master/ftpRelate/ftpClientReadTest1.py

1.2、上传文件

示例代码如下(ftpClientWriteTest1.py):

#! /usr/bin/env python
#-*- coding:utf-8 -*-

import ftplib

filename = "1.txt"
ftp = ftplib.FTP("127.0.0.1")
ftp.login() # user anonymous, passwd [email protected]
#ftp.login("UID", "PSW")
ftp.cwd("/pub")

with open(filename, 'r') as fin :
    ftp.storlines('STOR ' + filename, fin)

该代码的github地址: https://github.com/mike-zhang/pyExamples/blob/master/ftpRelate/ftpClientWriteTest1.py