Python FTP檔案傳輸實現

2021-08-01 09:06:05 字數 3214 閱讀 5609

ftp一般流程

ftp對應pasv和port兩種訪問方式,分別為被動和主動,是針對ftp伺服器端進行區分的,正常傳輸過程中21號埠用於指令傳輸,資料傳輸埠使用其他埠。

pasv:由客戶端發起資料傳輸請求,伺服器端返回並攜帶資料埠,並且伺服器端開始監聽此埠等待資料,為被動模式;

port:客戶端監聽埠並向伺服器端發起請求,伺服器端主動連線此埠進行資料傳輸,為主動模式。

其中type分兩種模式,i對應二進位制模式、a對應ascii模式;

pasv為客戶端傳送請求,之後227為伺服器端返回操作碼表示成功,並且後面帶有伺服器端監聽的埠:143x256(左移8位)+48

2. python**實現:

中文路徑問題:由於ftp支援ascii編碼,python ftplib中編碼方式使用latin-1,而window預設編碼方式為gbk,所以使用python處理時需先將中文路徑編碼為gbk之後解碼為latin-1字元;

從乙個ftp伺服器到另乙個ftp伺服器的資料傳輸:

首先登陸兩個ftp伺服器,transfercmd函式用於傳送命令並返回已建立好連線的本地socket,此時分別在兩個本地socket進行資料的收發即可。

在測試中發現,在傳送完乙個檔案之後只有及時的關閉socket,21埠才會返回226,資料完成指示,這樣才可以迴圈下乙個檔案,在完成之後要退出ftp。

#coding=utf-8

import ftplib,os.path,os

import socket

f1=ftplib.ftp('172.16.2.76')

f2=ftplib.ftp('172.16.25.153')

class myftp:

path='file/download/bbb/'

# ftplib中編碼使用latin-1

title='版本'.encode(encoding='gbk').decode(encoding='latin-1')

path1=path+title

localdir='e:\\ver\\fp\\'

path2='abc/edf/'

def __init__(self):

try:

f1.login('username','password')

except ftplib.error_perm:

print('f1 cannot loggin!')

return

try:

f2.login()

except ftplib.error_perm:

print('f2 cannot loggin!')

return

def ftpd(self):

filelist=

filelist=

f1.cwd(self.path1)

for file in filelist:

fileattr=file.split(' ')

filename=fileattr[-1]

filetype=fileattr[0][0]

if filetype=='-':

for file in filelist:

path=self.localdir+file

f1.retrbinary('retr %s'%(file),open(path,'wb').write)

print('%s download.....'%(file))

f1.quit()

def ftpu(self,fun=1):

os.chdir(self.localdir)

filelist=os.listdir()

# upload file

if fun==1:

for file in filelist:

path=self.path2

f2.storbinary('stor %s'%(path+file),open(file,'rb'))

print('%s uploading......'%(file))

#delete file

if fun==0:

try:

for file in filelist:

path=self.path2

f2.delete(path+file)

print('%s delete......'%(file))

except ftplib.error_perm:

print('no file to delete!!')

return

f2.quit()

def test(self):

f1.cwd(self.path1)

f2.cwd(self.path2)

filelist=f1.nlst()

print(filelist)

for file in filelist:

print('transfer %s......'%(file))

f1.voidcmd('type i')

f2.voidcmd('type i')

sock1=f1.transfercmd('retr %s'%(file))

sock2=f2.transfercmd('stor %s'%(file))

while 1:

data=sock1.recv(1024)

sock2.sendall(data)

if len(data)==0:

break

# 資料傳送完成後需關閉socket,伺服器21埠才會有返回

sock1.close()

sock2.close()

res1=f1.getresp()

#print('f1 >> %s'%(res1))

res2=f2.getresp()

#print('f2 >> %s'%(res2))

f1.quit()

f2.quit()

if __name__=='__main__':

ftptest=myftp()

ftptest.ftpu(0)

#ftptest.test()

#ftptest.ftpd()

python ftp檔案傳輸服務端

from socket import import os,sys import signal,time host port 8888 addr host,port self.c.send file.encode def getfilename self l os.listdir l1 for i i...

python實現檔案傳輸

我程式設計的時候,開了兩個python的shell,乙個做server,乙個做client 然後就直接在命令提示符下一行一行的編,感覺自己是在使用一套高階命令列,而不是在程式設計。server端 import socket sersock socket.socket socket.af inet,s...

TCP實現檔案傳輸

一直想著給之前的clouddisk專案加上乙個c s架構的檔案傳輸模組,因為之前是nginx fastcgi架構的b s架構,自己又不會前段 沒有辦法繼續增加新的功能塊。最近終於抽出時間開始寫專案了,已經選用tcp完成linux下的cs架構檔案上傳功能模組,這裡展示tcp檔案傳輸模組。socket類...