- 积分
- 3638
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2014-10-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 15195775117 于 2021-1-31 00:35 编辑
平时工作需要从局域网里的服务器拷贝数据,用的是XFTP软件,速度实在太慢了,
XFTP还容易崩溃,传输了一夜,第二天一看软件崩了,生气!
于是想着自己写一个ftp,速度慢些也罢了,主要是能自动重连,
从网上复制里些代码,看懂了,改了改:
from ftplib import FTP
import time
import tarfile
import os
from ftplib import FTP
def ftpconnect(host, username, password):
ftp = FTP()
# ftp.set_debuglevel(2)
ftp.connect(host, 21)
ftp.login(username, password)
return ftp
#从ftp下载文件
def downloadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'wb')
ftp.retrbinary('RETR ' + remotepath, fp.write, bufsize)
ftp.set_debuglevel(0)
fp.close()
#从本地上传文件到ftp
def uploadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'rb')
ftp.storbinary('STOR ' + remotepath, fp, bufsize)
ftp.set_debuglevel(0)
fp.close()
if __name__ == "__main__":
#以下3个可以从xftp中查:
host="xx.xx.x.xx"
username="xxxxxxx"
password="xxxxxxx"
ftp = ftpconnect(host,username,password)
# downloadfile(ftp, "/xxxxjson/x_xxxx/xxx/2020110107.xxxx", "C:/Users/Administrator/Desktop/get_ftp.txt")
# #调用本地播放器播放下载的视频:
# os.system('start "C:\Program Files\Windows Media Player\wmplayer.exe" "C:/Users/Administrator/Desktop/test.mp4"')
# uploadfile(ftp, "C:/Users/Administrator/Desktop/test.mp4", "test.mp4")
ftp.quit()
结果:Traceback (most recent call last): File "C:/Users/Administrator/Desktop/ftp_example.py", line 46, in <module> ftp = ftpconnect(host,username,password) File "C:/Users/Administrator/Desktop/ftp_example.py", line 19, in ftpconnect ftp.connect(host, 21) File "C:\ProgramData\Anaconda3\lib\ftplib.py", line 152, in connect self.sock = socket.create_connection((self.host, self.port), self.timeout, File "C:\ProgramData\Anaconda3\lib\socket.py", line 808, in create_connection raise err File "C:\ProgramData\Anaconda3\lib\socket.py", line 796, in create_connection sock.connect(sa)ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
|
|