#!/usr/bin/python
# coding=utf-8
from ftplib import FTP
import json
import time
import sys
import os
import platform
import hashlib
import random
import string
logger = None
class Ftpconnect():
def ftp_connect(self,host,username,password,timeout=300):
f = FTP()
f.connect(host,'21',timeout)
print f, host, username, password
f.login(username,password)
res=f.retrlines('LIST')
print res
return f
def ftp_download(self,f,file_name,bufsize=1024):
sys_type=platform.system()
print sys_type
if sys_type in 'Linux':
file_local = '/home/test/'+ file_name
else:
file_local='D:\\ftp\\'+ file_name
print file_local
fp=open(file_local,'wb')
f.retrbinary('RETR ' + file_local, fp.write, bufsize)
fp.close()
local_md5=hashlib.md5(open(file_local, 'rb').read()).hexdigest()
print local_md5
return local_md5
def ftp_upload(self,f,file_name,bufsize=1024):
new_file_name=string.join(random.sample(['a','b','c','d','e','f','g','h','i','j','k'], 5)).replace(" ","") + file_name
sys_type=platform.system()
print sys_type
if sys_type in 'Linux':
file_local = '/home/test/'+ file_name
else:
file_local='D:\\ftp\\'+ file_name
print file_local
fp = open(file_local, 'rb')
print fp
f.storbinary('STOR %s' % file_local, fp, bufsize)
fp.close()
local_md5=hashlib.md5(open(file_local, 'rb').read()).hexdigest()
return local_md5
def ftp_close(self,f):
return f.quit()
if __name__ == "__main__":
f=Ftpconnect().ftp_connect('192.168.35.134','ftpusr','supercxp@123')
Ftpconnect().ftp_download(f,'3.mp4')
os.system('start "C:\Program Files\Windows Media Player\wmplayer.exe" "D:\\ftp\\3.mp4"')
f.quit()