Python ftp上传文件

WBOY
Release: 2016-06-10 15:06:07
Original
1470 people have browsed it

以下代码比较简单,对python实现ftp上传文件相关知识感兴趣的朋友可以参考下

#encoding=utf8
from ftplib import FTP #加载ftp模块
IP = '103.240.150.104'
user = 'webmaster@stchat.cn'
password = '5'
filename = 'zhihu.html'
path = '/root/Desktop/zhihu.html'
ftp=FTP() #设置变量
ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.connect(IP) #连接的ftp sever和端口
ftp.login(user,password)#连接的用户名,密码
print ftp.getwelcome() #打印出欢迎信息
ftp.storbinary('STOR %s'%filename, open(path, 'rb',8192)) 
print('success')
Copy after login

下面给大家介绍python实现ftp上传下载文件的方法

python本身自带一个FTP模块,可以实现上传下载的函数功能。

import ftplib 
ftp = ftblib.FTP("ftp.yourServer.com") 
ftp.login("username","password") 
filename = "index.html" 
ftp.storlines("STOR "+filename,open(filename)) 
filename="app.exe" 
ftp.storbinary("STOR "+filename,open(filename,"rb"),1024) 
#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
from ftplib import FTP 
def ftp_up(filename = "20120904.rar"): 
ftp=FTP() 
ftp.set_debuglevel(2) 
#打开调试级别2,显示详细信息;0为关闭调试信息 
ftp.connect('192.168.0.1','21') 
#连接 
ftp.login('admin','admin') 
#登录,如果匿名登录则用空串代替即可 
#print ftp.getwelcome() 
#显示ftp服务器欢迎信息 
#ftp.cwd('xxx/xxx/') 
#选择操作目录 
bufsize = 1024 
#设置缓冲块大小 
file_handler = open(filename,'rb') 
#以读模式在本地打开文件 
ftp.storbinary('STOR %s' % os.path.basename(filename),file_handler,bufsize) 
#上传文件 
ftp.set_debuglevel(0) 
file_handler.close() 
ftp.quit() 
print "ftp up OK" 
def ftp_down(filename = "20120904.rar"): 
ftp=FTP() 
ftp.set_debuglevel(2) 
ftp.connect('192.168.0.1','21') 
ftp.login('admin','admin') 
#print ftp.getwelcome() 
#显示ftp服务器欢迎信息 
#ftp.cwd('xxx/xxx/') 
#选择操作目录 
bufsize = 1024 
filename = "20120904.rar" 
file_handler = open(filename,'wb').write 
#以写模式在本地打开文件 
ftp.retrbinary('RETR %s' % os.path.basename(filename),file_handler,bufsize) 
#接收服务器上文件并写入本地文件 
ftp.set_debuglevel(0) 
file_handler.close() 
ftp.quit() 
print "ftp down OK" 
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template