pythonftp
⑴ python ftp模塊有支持passive off這個命令嗎
因為FTP有兩種工作模式,PORT方式和PASV方式,中文意思為主動式和被動式,詳細介紹如下:主動FTP:命令連接:客戶端>1024埠→伺服器21埠數據連接:客戶端>1024埠←伺服器20埠被動FTP:命令連接:客戶端>1024埠→伺服器21埠數據連接:客戶端>1024埠←伺服器>1024埠PORT(主動)方式的連接過程是:客戶端向伺服器的FTP埠(默認是21)發送連接請求,伺服器接受連接,建立一條命令鏈路。當需要傳送數據時,客戶端在命令鏈路上用PORT命令告訴伺服器:「我打開了***X埠,你過來連接我」。於是伺服器從20埠向客戶端的***X埠發送連接請求,建立一條數據鏈路來傳送數據。PASV(被動)方式的連接過程是:客戶端向伺服器的FTP埠(默認是21)發送連接請求,伺服器接受連接,建立一條命令鏈路。當需要傳送數據時,伺服器在命令鏈路上用PASV命令告訴客戶端:「我打開了***X埠,你過來連接我」。於是客戶端向伺服器的***X埠發送連接請求,建立一條數據鏈路來傳送數據。解決此問題的法也很簡單,關閉客戶端的PASV方式,強制其用PORT方式訪問伺服器,登錄FTP伺服器後用passive命令關閉客戶端的PASV方式,如下:ftp>passivePassivemodeoff.ftp>passive(再次運行命令可打開)Passivemodeon.
⑵ python查看FTP是否能連接成功的方法
#!/usr/local/bin/python
#-*- coding: UTF-8 -*-
####################################################################
# python 查看FTP是否能連接成功
####################################################################
#import ftplib
##f = ftplib.FTP('192.168.100.214')
##f.login('GDATA_MEM','123456')
##ftplib.set_debuglevel(2) #打開調試級別2,顯示詳細信息
#data=ftplib.FTP('127.0.0.1','admin','123456')
#print data
from ftplib import FTP
def ftp_open(host='',user='',passwd=''): #打開FTP
try:
ftp=FTP()
) #打開調試級別2,顯示詳細信息
) #連接
data=ftp.login(user,passwd) #登錄,如果匿名登錄則用空串代替即可
print () #歡迎詞
#print data
return 1
except:
return 0
if __name__=='__main__':
if ftp_open('127.0.0.1','admin','1234561'): #打開FTP
print "打開FTP成功"
else:
print "打開FTP失敗"
⑶ python ftp連接指定伺服器,並下載指定文件
ftp客戶端一般只有顯示、上傳、下載,並沒有打開文件的操作,所以你得先下載相應配置文件,在本地解析後,再繼續用相關參數下載後續文件。
⑷ python,如何下載FTP上的文件
importftplib,socket
defconnect():
CONST_HOST="xxxx.xxxx.xxx"
CONST_USERNAME="xxxxx"
CONST_PWD="xxxxxxx"
try:
ftp=ftplib.FTP(CONST_HOST)
ftp.login(CONST_USERNAME,CONST_PWD)
returnftp
exceptsocket.error,socket.gaierror:
print("FTPisunavailable,pleasecheckthehost,usernameandpassword!")
sys.exit(0)
defdisconnect(ftp):
ftp.quit()
defdownload(ftp,filename):
#預定義每次寫文件的buffer
CONST_BUFFER_SIZE=8192
f=open(filename,"wb").write
try:
ftp.retrbinary("RETR%s"%filename,f,CONST_BUFFER_SIZE)
exceptftplib.error_perm:
returnFalse
returnTrue
deffind(ftp,filename):
ftp_f_list=ftp.nlst()
iffilenameinftp_f_list:
returnTrue
else:
returnFalse
ftp=connect()
#yourfile為你想要下載的文件
iffind(ftp,'yourfile'):
download(ftp,'yourfile')
disconnect(ftp)
⑸ Python中如何獲取FTP伺服器上的文件大小
這要看你下載用了哪個ftp庫
搜到的代碼
files = ftp.nlst()
for f in files:
print f
⑹ 如何用python寫ftp伺服器
參考下面的連接:
http://www.open-open.com/lib/view/open1326808627280.html
http://blog.csdn.net/lf8289/article/details/2743269
⑺ 如何在Python中使用FTP
from ftplip import FTP # 首先要引入 ftplib 這個庫
# 接著,這是我自定義的幾個用於操作 ftp 的函數,def 後面跟的是自定義函數名,函數體內FTP 相關的語句和函數來自上面 import 那個包,你自己體會吧:
# 初始化 FTP 連接
def ftp_open(HOST,PORT,USERNAME,PASSWORD):
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect(host=HOST,port=PORT)
ftp.login(USERNAME,PASSWORD)
return ftp
# 關閉 FTP 連接
def ftp_close(ftp):
ftp.set_debuglevel(0)
ftp.quit()
# 文件上傳函數,filename 可以是windows 格式包含全路徑的文件
def ftp_up(ftp,filename):
#print(ftp.getwelcome())
#ftp.cwd('xxx/www')
file_handler=open(filename,'rb')
ftp.storbinary('STOR %s' % os.path.basename(filename),file_handler)
file_handler.close()
print('FTP up OK.')
# 文件下載函數,filename 可以是windows 格式包含全路徑的文件
def ftp_down(ftp,filename):
#print(ftp.getwelcome())
#ftp.cwd('xxx/www')
file_handler=open(filename,'wb')
ftp.retrbinary('RETR %s' % os.path.basename(filename),file_handler)
file_handler.close()
print('FTP down OK.')
⑻ python如何創建FTP目錄
直接使用ftplib就可以實現,其中有一個nlst功能用於查詢目錄,cwd用於改變目錄,還有一個size功能。你這邊只需要保留一個目錄的鏡像。如果發現有目錄差異,或者是文件大小發生變更,就用retrbinary進行文件同步就可以。
⑼ python ftp permission denied怎麼解決
FTP 550 Permission denied 只能建文件夾,沒法刪除及上傳文件的解決方法,需要的朋友可以參考下。
出現這種問題,
①首先檢查用戶對應的角色名,然後看路徑設置是否正確,有沒有相關的許可權,如果這些都沒問題,然後就嘗試下面的操作:
②重起FTP服務,最好從服務裡面重啟(不能根本解決);
③重新設置Server-U帳號和目錄訪問里的文件路徑(如果還不行,嘗試下面操作);
④刪除用戶,重新建。
原創 SU整理(FTP)收藏
整理如下:
1開頭-成功
2開頭-成功
3開頭-許可權問題
4開頭-文件問題
5開頭-伺服器問題
150 FILE: %s
150 Opening %s mode data connection for %s (%I64d Bytes).
150 Opening %s mode data connection for %s.
150 Opening ASCII mode data connection for /bin/ls.
150 Opening BINARY mode data connection for MLSD.
200 (end of 'index %s')
200 Command channel switched to clear-text.
200 Command OK, %I64d Bytes available on %s
200 Command okay.
200 EXEC command successful (TID=%u).
200 Message received okay.
200 MLST OPTS
200 MODE S ok.
200 MODE Z LEVEL set to %d.
200 MODE Z ok.
200 Noted.
200 Opened OK (but not really).
200 PBSZ command OK. Protection buffer size set to 0.
200 PBSZ=0
200 PORT Command successful.
200 PROT command OK. Using clear data connection.
200 PROT command OK. Using private data connection.
200 SSCN:CLIENT METHOD
200 SSCN:SERVER METHOD
200 STRU F ok.
200 STRU R ok.
200 Transfer progress notification is off.
200 Transfer progress notification is on.
200 Type set to A.
200 Type set to I.
202 Command not implemented, superfluous at this site.
202 Command not implemented, superfluous at this site.
210 UTC%+ld
211 End
211 End of status.
211-Extension supported
211-Status for user %Name from %IP:
213 %I64d
213 %s
214 Direct comments or bugs to [email protected].
214- The following commands are recognized (* => unimplemented).
215 UNIX Type: L8
220 Service ready for new user.
220 Serv-U FTP Server v%s for WinSock ready...
221 Goodbye!
226 ABOR command successful.
226 Transfer complete.
226 Transfer complete.
226 Transfer complete.
227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)
230 Password changed okay.
230 Switching to SYSTEM MAINTENANCE mode.
230 User logged in, proceed.
230 User logged in.
231 Switching to GROUP MAINTENANCE mode.
232 Switching to DOMAIN MAINTENANCE mode.
233 Switching to READ-ONLY MAINTENANCE mode.
233 User logged in, PASSWORD EXPIRED - please change before proceeding.
234 AUTH command OK. Already have SSL connection.
234 AUTH command OK. Initializing SSL connection.
250 %X
250 DELE command successful.
250 Directory changed to %s
250 End.
250 RMD command successful.
250 RNTO command successful.
250-Listing %s
253 Attributes changed okay.
253 Date/time changed okay.
257 "%s" directory created.
257 "%s" is current directory.
321 Transferred %I64d Bytes.
331 Response to otp-md%d %u %s required for skey.
331 User name okay, need password.
331 User name okay, please send complete E-mail address as password.
350 File or directory exists, ready for destination name
350 Restarting at %I64d. Send STORE or RETRIEVE.
421 Data connection closed (SSL/TLS negotiation failed).
421 Service closing control connection.
425 Cannot open data connection.
425 Cannot open data connection.
425 Cannot open data connection.
425 Try later, data connection in use.
425 Try later, data connection in use.
425 Try later, data connection in use.
425 Try later, out of network sockets.
425 Try later, out of network sockets.
425 Try later, out of network sockets.
425 Unable to listen, too many pending PASV requests from same client IP.
425 Unable to set up passive listening socket.
426 Data connection closed, cannot read file %s.
426 Data connection closed, cannot write file %s - disk full?
426 Data connection closed, error compressing data stream.
426 Data connection closed, error decompressing data stream.
426 Data connection closed, file transfer %s aborted by client.
426 Data connection closed, file transfer %s aborted.
426 Data connection closed, receive file %s aborted.
426 Data connection closed, transfer aborted.
426 Transfer aborted.
431 Account temporarily locked for other user, please try later.
431 Password not changed, no match for old password.
431 Unable to negotiate secure command connection.
432 Password not changed, new password should be at least %d characters in length.
432 Password not changed, new password should contain at least one upper-case character and symbol
432 Password not changed, old and new passwords cannot be identical.
450 Cannot open file %s for reading.
452 Sorry, insufficient disk quota - receive file %s aborted.
500 '%s': command not understood.
501 %s: Not a directory.
501 Cannot EXEC command line (error=%u).
501 Invalid MLST options
501 Invalid option.
501 Not enough space, only %I64d Bytes available on drive %s
501 Permission denied.
501 SITE option not supported.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Wildcards not allowed.
502 Syntax error - use SITE PSWD "<oldpassword>" "<newpassword>".
503 Bad sequence of commands.
503 Bad sequence of commands.
503 Secure command connection required prior to PBSZ command.
503 Secure command connection required prior to PROT command.
504 Command not implemented for that parameter.
504 Command not implemented for that parameter.
504 Command not implemented for that parameter.
504 Unknown security mechanism.
510 Cannot determine server time zone.
521 Data connection cannot be opened with this PROT setting.
522 SSL/TLS lib refuses to initiate secure data connection.
530 Cannot change Anonymous password.
530 Cannot change password.
530 Not logged in, cannot find home directory.
530 Not logged in, home directory does not exist.