這是乙個Scanner

2021-08-28 23:25:49 字數 2226 閱讀 6152

最近,老師讓寫乙個ftp掃瞄器並匿名登陸。要求掃瞄到所有的檔案。

預備知識:

ftplib的使用

python異常處理知識

字串處理技巧(牽扯到拼接路徑)

遞迴程式設計技巧(需要用遞迴來遍歷所有檔案)

#思路通過思考問題,我設計的遞迴引數是將父目錄當作引數。這個是肯定存在的。所以不需要進行異常處理

有以下幾種情況:

1、父目錄為空,所以要pwd到父目錄的上一級目錄

2、父目錄只存在檔案,不存在資料夾 這種要讀取所有的檔案,然後返回上一層

所以,開始寫**:(**還不夠完善,後期再說)

#函式功能:遞迴遍歷每乙個檔案

# 如果parent為空,返回上層目錄

# 如果dir列為空,輸出所有的docs列,返回上層目錄

# 如果dir列不為空,遞迴遍歷dir列 然後輸出所有的docs列,返回上層目錄

import ftplib

from ftplib import *

ftp = ftp('202.204.121.124')

ftp.login()

ftp.encoding = 'utf8'

# 拿到上層目錄的路徑

def getpwd(parent):

total = 0

for i in parent:

if i == '/':

total += 1

if total == 1:

return '/'

ls = parent[::-1]

for index,ori in enumerate(ls):

if ori == '/':

real = ls[index+1:]

real = real[::-1]

return real

#遞迴遍歷父目錄

def digui(parent):

ftp.cwd(parent)

alls = ftp.nlst()

direcs =

docs =

# 如果這個資料夾為空 則將pwd置為它的根目錄

if len(alls) == 0:

pwd = getpwd(ftp.pwd().encode('latin1').decode())

ftp.cwd(pwd)

return

# 將parent下的東西分好類,分別裝到direcs和docs

for i in alls:

try:

ftp.cwd(i)

except ftplib.error_perm:

else:

ftp.cwd(parent) # 別忘了cwd回parent 不然就錯了

if len(direcs):

for i in direcs:

if ftp.pwd().encode('latin1').decode() == '/':

digui(ftp.pwd().encode('latin1').decode() + i)

else:

digui(ftp.pwd().encode('latin1').decode() + '/' + i)

for i in docs:

if ftp.pwd().encode('latin1').decode() == '/':

print(ftp.pwd().encode('latin1').decode() + i)

else:

print(ftp.pwd().encode('latin1').decode() + '/' + i)

pwd = getpwd(ftp.pwd().encode('latin1').decode())

ftp.cwd(pwd)

else:

for i in docs:

if ftp.pwd().encode('latin1').decode() == '/':

print(ftp.pwd().encode('latin1').decode() + i)

else:

print(ftp.pwd().encode('latin1').decode() + '/' + i)

pwd = getpwd(ftp.pwd().encode('latin1').decode())

ftp.cwd(pwd)

digui('/')

這是乙個開始

準備正式的應對職業之路,是從劉未鵬的部落格開始 在此之前,從事的工作比較漫不經心,因為不熱愛的緣故,也只是工作而已 經朋友推薦,瀏覽了劉的部落格,一下仿似給自己找到了乙個出口。長期以來自己不成條理,漫無目標的思考,找到了乙個可以依附的體系 在新下來的日子裡,我認真的做事,努力學習周邊,在漂亮完成兩個...

這是乙個隨筆

自我介紹 21160635 我的名字是曾磊鑫 愛好玩遊戲和聽 最喜歡吃的就是老黃牛的麻辣香鍋 晉級賽bo5又輸了xd 問題簡答 1 回想一下你初入大學時對本專業的暢想 1.當初你是如何做出選擇軟體工程專業的決定的?年輕不懂事當初以為選了軟工就可以在大學有理由好好玩遊戲了,現在發現家裡沒礦學業要緊。2...

這是乙個ibatIS xml檔案

msg id eqmsgid bigint msg id in inmsgids msg id in inmsgidlist bigint account id eqaccountid bigint account id gtaccountid bigint msg type eqmsgtype v...