Python脚本通过FTP上传文件 [英] Python Script Uploading files via FTP

查看:417
本文介绍了Python脚本通过FTP上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个脚本将文件上传到FTP。



登录系统如何工作?我正在寻找这样的东西:

  ftp.login =(mylogin)
ftp.pass =(mypass )

以及其他任何登录凭证。 h2_lin>解决方案

使用 ftplib 可以这样写:

  import ftplib 
session = ftplib.FTP('server。 ('''','''''''''''''''''''''''''')文件)#发送文件
file.close()#关闭文件和FTP
session.quit()

如果您的FTP主机需要TLS,请使用 ftplib.FTP_TLS




要检索它,您可以使用 urllib。检索

  import urllib 

urllib.urlretrieve('ftp:// server / path / to / file','file')






编辑

要找到当前目录,请使用 FTP.pwd()


FTP.pwd():返回服务器上当前目录的路径名。 / p>

要更改目录,请使用 $ b


FTP.cwd(路径名):设置服务器上的当前目录。



I would like to make a script to upload a file to FTP.

How would the login system work? I'm looking for something like this:

ftp.login=(mylogin)
ftp.pass=(mypass)

And any other sign in credentials.

解决方案

Use ftplib, you can write it like this:

import ftplib
session = ftplib.FTP('server.address.com','USERNAME','PASSWORD')
file = open('kitten.jpg','rb')                  # file to send
session.storbinary('STOR kitten.jpg', file)     # send the file
file.close()                                    # close file and FTP
session.quit()

Use ftplib.FTP_TLS instead if you FTP host requires TLS.


To retrieve it, you can use urllib.retrieve:

import urllib 

urllib.urlretrieve('ftp://server/path/to/file', 'file')


EDIT:

To find out the current directory, use FTP.pwd():

FTP.pwd(): Return the pathname of the current directory on the server.

To change the directory, use FTP.cwd(pathname):

FTP.cwd(pathname): Set the current directory on the server.

这篇关于Python脚本通过FTP上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆