Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >python 上传所有文件到FTP服务器 脚本

python 上传所有文件到FTP服务器 脚本

作者头像
用户5760343
发布于 2022-05-13 01:51:38
发布于 2022-05-13 01:51:38
2.1K00
代码可运行
举报
文章被收录于专栏:sktjsktj
运行总次数:0
代码可运行

!/bin/env python

import os, sys, ftplib from getpass import getpass from mimetypes import guess_type

nonpassive = False # passive FTP by default remotesite = 'learning-python.com' # upload to this site remotedir = 'books' # from machine running on remoteuser = 'lutz' remotepass = getpass('Password for %s on %s: ' % (remoteuser, remotesite)) localdir = (len(sys.argv) > 1 and sys.argv[1]) or '.' cleanall = input('Clean remote directory first? ')[:1] in ['y', 'Y']

print('connecting...') connection = ftplib.FTP(remotesite) # connect to FTP site connection.login(remoteuser, remotepass) # log in as user/password connection.cwd(remotedir) # cd to directory to copy if nonpassive: # force active mode FTP connection.set_pasv(False) # most servers do passive

if cleanall: for remotename in connection.nlst(): # try to delete all remotes try: # first, to remove old files print('deleting remote', remotename) connection.delete(remotename) # skips . and .. if attempted except: print('cannot delete remote', remotename)

count = 0 # upload all local files localfiles = os.listdir(localdir) # listdir() strips dir path # any failure ends script for localname in localfiles: mimetype, encoding = guess_type(localname) # e.g., ('text/plain', 'gzip') mimetype = mimetype or '?/?' # may be (None, None) maintype = mimetype.split('/')[0] # .jpg ('image/jpeg', None')

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
localpath = os.path.join(localdir, localname)
print('uploading', localpath, 'to', localname, end=' ')
print('as', maintype, encoding or '')

if maintype == 'text' and encoding == None:
    # use ascii mode xfer and bytes file
    # need rb mode for ftplib's crlf logic
    localfile = open(localpath, 'rb')
    connection.storlines('STOR ' + localname, localfile)

else:
    # use binary mode xfer and bytes file
    localfile = open(localpath, 'rb')
    connection.storbinary('STOR ' + localname, localfile)

localfile.close()
count += 1

connection.quit() print('Done:', count, 'files uploaded.')

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
python ftp工具类 上传、下载文件夹 封装类
-------------------------常用工具------------------------------------------------------------
用户5760343
2022/05/13
1.3K0
python 下载FTP服务器所有文件 封装类
import os, sys, ftplib from getpass import getpass from mimetypes import guess_type, add_type
用户5760343
2022/05/13
1.2K0
python ftp 下载所有文件到本地 脚本
import os, sys, ftplib from getpass import getpass from mimetypes import guess_type
用户5760343
2022/05/13
1.9K0
python 互联网编程
zope\plone\pylons\web2py\cherrypy\webware:web框架 pyjamas soap:web service Ironpython 1\ socket(AF_INET,SOCK_STREAM) SOCK_DGRAM AF_UNIF sobj.bind() sobj.listen con,dre=sobj.accept() con.receive() con.send() con.close() socket(xx) sobj.connect()
用户5760343
2022/05/13
4360
python 互联网编程
python ftp下载文件 脚本
import os, sys from getpass import getpass # hidden password input from ftplib import FTP # socket-based FTP tools
用户5760343
2022/05/13
9960
python ftp上传文件 脚本
import ftplib # socket-based FTP tools
用户5760343
2022/05/13
5.5K0
python ftp测试
#!/usr/bin/env python import time from ftplib import FTP local_dir_update="*****" local_dir_download="*******" remote_host="*.*.*.*" port="21" remote_user="name" remote_passwd="password" ftp=FTP() ftp.set_debuglevel(2) ftp.connect(remote_host,port) ftp.login(remote_user,remote_passwd) print ftp.getwelcome() #ftp.cwd() bufsize = 1024 filename='ha.txt' file_handler = open(filename,'rb') ftp.storbinary('STOR ha.txt',file_handler,bufsize) ftp.set_debuglevel(0) file_handler.close() ftp.quit()
py3study
2020/01/13
1.3K0
Python 基于 FTP 历史版本一键部署
(1)、需要接收一个日期的格式参数,以便从 ftp 上面去获取相应的历史版本部署包,获取到历史部署包后会在本地解压并且解决下载的原始压缩包
Wu_Candy
2024/03/12
1320
Python 基于 FTP 历史版本一键部署
python搭建FTP服务器之FTP上传和下载
Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,函数列举如下
周小董
2019/03/25
10.8K0
Python 基于Python实现Ftp文件上传,下载
支持FTP文件上传、下载,可以上传目录(分区除外),也可以上传单个文件;可以下载整个目录(/根目录除外),也可以下载单个文件
授客
2019/09/11
5.6K0
Python 基于Python实现Ftp文件上传,下载
python ftp
完全是循环,目录的进行循环操作,而文件下载。最底层目录的文件下载完,回归上级目录。继续循环。
py3study
2020/01/09
9.2K0
ftp工具类:上传与下载文件
linux服务器搭建ftp服务: https://program.blog.csdn.net/article/details/88825921
全栈程序员站长
2022/08/26
4K0
python脚本之ftp上传日志
因为ssoc日志巨大,很快就把磁盘占满。需要每天把备份上传到ftp服务器上,所以根据网上的资料,做了个简单的脚本。算是第一次自己拼凑出的脚本。还很简单,特别是把异常处理简化了。因为本身单一,然后把屏幕输出用管道命令》直接写到本地文件,充当日志。很懒的一个版本。还需加工。
py3study
2020/01/07
1.8K0
python ftp和sftp的例子
YESTERDAY = TODAY - datetime.timedelta(days=1)
py3study
2020/01/07
1.4K0
CentOS 6.5系统下构建FTP服务器
一台Linux Server ip 192.168.1.254,一台Linux Client ip 192.168.1.100
星哥玩云
2022/07/01
6700
【bypy】python进行百度云下载【未完待续】
安装 pip install bypy 然后,登录 bypy info 复制这个连接,打开并登录百度账户 进行授权 bypy -h 帮助信息 root@HeannysPi:~# bypy -h usage: bypy [-h] [-V] [-d] [-v] [-r RETRY] [-q] [-t TIMEOUT] [-s SLICE]             [--chunk CHUNK] [-e] [-f] [--no-resume-download]             [--includ
一朵灼灼华
2022/08/05
7110
【bypy】python进行百度云下载【未完待续】
Python搭建FTP服务器
使用的ftp包:pyftpdlib    pip install pyftpdlib就可以下载安装了
py3study
2020/01/09
6.8K0
VC++ libcurl FTP上传客户端程序
最近需要在Windows下使用libcurl库实现FTP文件上传的MFC程序,最基础的一个版本的功能是定时扫描某个目录下符合文件规则(比如*.json *.xml等)的所有文件(包括子目录),然后将其上传到某个FTP目录(需要配置好上传的FTP账号信息,比如FTP目录,FTP账号、密码、),类似如下面的XML信息:
ccf19881030
2020/08/19
3K0
VC++ libcurl FTP上传客户端程序
python实现ftp上传
remoutpath='/ebossdata02/eboss/fileservice/bass/mms/'
py3study
2020/01/08
3.6K0
python ftp下载文件 脚本
------------------------------------------getfile.py
用户5760343
2022/05/13
1.2K0
相关推荐
python ftp工具类 上传、下载文件夹 封装类
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档