-
大小: 1.85 KB文件類(lèi)型: .rar金幣: 1下載: 0 次發(fā)布日期: 2024-12-20
- 語(yǔ)言: 其他
- 標(biāo)簽: python??ftp??自動(dòng)上傳??自動(dòng)下載??
資源簡(jiǎn)介
python實(shí)現(xiàn)的,借鑒的網(wǎng)上的代碼,但修改并測(cè)試通過(guò)了。函數(shù)已經(jīng)包裝好。修改修改可用于各種場(chǎng)合。
代碼片段和文件信息
#!/usr/bin/python
#coding=gbk
‘‘‘
ftp自動(dòng)下載、自動(dòng)上傳腳本,可以遞歸目錄操作
‘‘‘
from?ftplib?import?FTP
import?ossysstringdatetimetime
import?socket
class?MYFTP:
def?__init__(self?hostaddr?username?password?remotedir?port=21):
self.hostaddr?=?hostaddr
self.username?=?username
self.password?=?password
self.remotedir??=?remotedir
self.port?????=?port
self.ftp??????=?FTP()
self.file_list?=?[]
#?self.ftp.set_debuglevel(2)
def?__del__(self):
self.ftp.close()
#?self.ftp.set_debuglevel(0)
def?login(self):
ftp?=?self.ftp
try:?
timeout?=?60
socket.setdefaulttimeout(timeout)
ftp.set_pasv(True)
print?‘開(kāi)始連接到?%s‘?%(self.hostaddr)
ftp.connect(self.hostaddr?self.port)
print?‘成功連接到?%s‘?%(self.hostaddr)
print?‘開(kāi)始登錄到?%s‘?%(self.hostaddr)
ftp.login(self.username?self.password)
print?‘成功登錄到?%s‘?%(self.hostaddr)
debug_print(ftp.getwelcome())
except?Exception:
deal_error(“連接或登錄失敗“)
try:
ftp.cwd(self.remotedir)
except(Exception):
deal_error(‘切換目錄失敗‘)
def?is_same_size(self?localfile?remotefile):
try:
remotefile_size?=?self.ftp.size(remotefile)
except:
remotefile_size?=?-1
try:
localfile_size?=?os.path.getsize(localfile)
except:
localfile_size?=?-1
debug_print(‘lo:%d??re:%d‘?%(localfile_size?remotefile_size))
if?remotefile_size?==?localfile_size:
? return?1
else:
return?0
def?download_file(self?localfile?remotefile):
if?self.is_same_size(localfile?remotefile):
? debug_print(‘%s?文件大小相同,無(wú)需下載‘?%localfile)
? return
else:
debug_print(‘>>>>>>>>>>>>下載文件?%s?...?...‘?%localfile)
#return
file_handler?=?open(localfile?‘wb‘)
self.ftp.retrbinary(‘RETR?%s‘%(remotefile)?file_handler.write)
file_handler.close()
def?download_files(self?localdir=‘./‘?remotedir=‘./‘):
try:
self.ftp.cwd(remotedir)
except:
debug_print(‘目錄%s不存在,繼續(xù)...‘?%remotedir)
return
if?not?os.path.isdir(localdir):
os.makedirs(localdir)
debug_print(‘切換至目錄?%s‘?%self.ftp.pwd())
self.file_list?=?[]
self.ftp.dir(self.get_file_list)
remotenames?=?self.file_list
#print(remotenames)
#return
for?item?in?remotenames:
filetype?=?item[0]
filename?=?item[1]
local?=?os.path.join(localdir?filename)
if?filetype?==?‘d‘:
self.download_files(local?filename)
elif?filetype?==?‘-‘:
self.download_file(local?filename)
self.ftp.cwd(‘..‘)
debug_print(‘返回上層目錄?%s‘?%self.ftp.pwd())
def?upload_file(self?localfile?remotefile):
if?not?os.path.isfile(localfile):
return
if?self.is_same_size(localfile?remotefile):
? debug_print(‘跳過(guò)[相等]:?%s‘?%localfile)
? return
file_handler?=?open(localfile?‘rb‘)
self.ftp.storbinary(‘STOR?%s‘?%remotefile?file_handler)
file_handler.close()
debug_print(‘已傳送:?%s‘?%localfile)
def?upload_files(self?localdir=‘./‘?remotedir?=?‘./‘):
if?not?os.path.isdir(localdir):
return
localnames?=?os.listdir(localdir)
self.ftp.cwd(remotedir)
for?item?in?localnames:
src?=?os.path.join(localdir?item)
if?os.pa
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件???????4536??2010-12-08?18:01??autoftp\autoftp.py
?????文件??????????0??2010-12-08?18:01??autoftp\log.txt
?????文件?????????33??2010-12-08?17:54??autoftp\run.bat
?????目錄??????????0??2010-12-08?17:58??autoftp
-----------?---------??----------?-----??----
?????????????????4569????????????????????4
- 上一篇:在大圖中查找小圖顯示位置
- 下一篇:黑白棋(帶簡(jiǎn)單AI)
評(píng)論
共有 條評(píng)論