1. 程式人生 > >ftp上傳文件封裝

ftp上傳文件封裝

特殊字符 日誌 con dst share open xe8 \n 封裝

業務需求,頻繁傳遞圖片到對方ftp服務器,用此代碼可流式上傳文件。

上傳代碼

 1 # coding:utf-8
 2 import ftplib
 3 import logging
 4 import traceback
 5 
 6 def ftpLogin():
 7     lasterr = None
 8     for i in range(3):
 9         try:
10             ftp = ftplib.FTP()
11             ftp.connect(
12                 host=xx.xx.xx.xx,
13 port=21, 14 timeout=3 15 ) 16 ftp.login( 17 user=xxx, 18 passwd=xxx 19 ) 20 return ftp 21 except Exception: 22 lasterr = traceback.format_exc() 23 print error except when ftp connect, err: %s
% lasterr) 24 return None 25 26 def ftpCwd(ftp=None, path=/): 27 try: 28 ftp.cwd(path) 29 return True 30 except Exception: 31 try: 32 ftp.mkd(path) 33 ftp.cwd(path) 34 return True 35 except ftplib.error_perm: 36 print
error except when ftp cwd, err: %s % traceback.format_exc() 37 return False 38 39 def ftpUpfile(ftp=None, srcpath=None, dstfile=None): 40 bufsize = 1024 41 try: 42 with open(srcpath, rb) as fp: 43 cmd = STOR + dstfile 44 ftp.storbinary(cmd, fp, bufsize) 45 ftp.close() 46 return True 47 except Exception: 48 print error except when ftp put file,err: %s % traceback.format_exc() 49 return False 50 51 if __name__ == __main__: 52 file = a.txt 53 ftp = ftpLogin() 54 ftp.set_debuglevel(2) 55 ftp.set_pasv(0) 56 if ftp is not None: 57 cwd = ftpCwd(ftp=ftp, path=aa) 58 if cwd: 59 ret = ftpUpfile(ftp=ftp, srcpath=file, dstfile=file) 60 if ret: 61 logging.info(put file success) 62 ftp.close()

ftp debug內容:

*cmd* CWD aa
*put* CWD aa\r\n
*get* 250 Directory successfully changed.\r\n
*resp* 250 Directory successfully changed.
*cmd* TYPE I
*put* TYPE I\r\n
*get* 200 Switching to Binary mode.\r\n
*resp* 200 Switching to Binary mode.
*cmd* PORT 10,10,4,48,237,115
*put* PORT 10,10,4,48,237,115\r\n
*get* 200 PORT command successful. Consider using PASV.\r\n
*resp* 200 PORT command successful. Consider using PASV.
*cmd* STOR 1535340773614_\xe4\xb8\xb9\xe4\xb8\x9c_ _0_0_\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d_02_\xe9\x84\x82AW0613_1_SM_000006_1_1_ _ .jpg
*put* STOR 1535340773614_\xe4\xb8\xb9\xe4\xb8\x9c_ _0_0_\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d_02_\xe9\x84\x82AW0613_1_SM_000006_1_1_ _ .jpg\r\n
*get* 150 Ok to send data.\r\n
*resp* 150 Ok to send data.
*get* 226 Transfer complete.\r\n
*resp* 226 Transfer complete.

但是出現了一個很奇怪的現象:

中文目錄上傳帶特殊字符的中文圖片,代碼報錯但圖片卻上傳了ftp服務器,很詭異。

# python 日誌
2018-09-06 17:49:05,794 pushProcess.py[line:76] ERROR error except when ftp put file,err: Traceback (most recent call last):
  File "/home/seemmo/tongliao_push/seemmo/business/pushProcess.py", line 72, in ftpUpfile
    ftp.storbinary(cmd, fp, bufsize)
  File "/home/seemmo/share/python/python-2.6/Lib/ftplib.py", line 452, in storbinary
    conn = self.transfercmd(cmd)
  File "/home/seemmo/share/python/python-2.6/Lib/ftplib.py", line 360, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/home/seemmo/share/python/python-2.6/Lib/ftplib.py", line 344, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "/home/seemmo/share/python/python-2.6/Lib/ftplib.py", line 243, in sendcmd
    return self.getresp()
  File "/home/seemmo/share/python/python-2.6/Lib/ftplib.py", line 218, in getresp
    raise error_perm, resp
error_perm: 550 1536225485000_?¤§?1?é??é€?623KM+600M ?”±?????‘è¥?_ _0_0_??a?3???‰?…¨??|_02_è??ALN838_1_SM_000006_1_2_ _ .jpg: Cannot create file.

# ftp debug
*cmd* CWD \xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d
*put* CWD \xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d\r\n
*get* 250 Directory changed to /\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d\\\r\n
*resp* 250 Directory changed to /\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d\\
*cmd* TYPE I
*put* TYPE I\r\n
*get* 200 Type set to I.\r\n
*resp* 200 Type set to I.
*cmd* PORT 10,102,167,241,161,167
*put* PORT 10,102,167,241,161,167\r\n
*get* 200 PORT Command successful.\r\n
*resp* 200 PORT Command successful.
*cmd* STOR 1536225052000_G45\xe9\xab\x98\xe9\x80\x9f479KM 100M\xe7\x94\xb1\xe4\xb8\x9c\xe5\x90\x91\xe8\xa5\xbf_ _0_0_\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d_02_\xe8\xbe\xbdA382KU_1_SM_000006_1_1_ _ .jpg
*put* STOR 1536225052000_G45\xe9\xab\x98\xe9\x80\x9f479KM 100M\xe7\x94\xb1\xe4\xb8\x9c\xe5\x90\x91\xe8\xa5\xbf_ _0_0_\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d_02_\xe8\xbe\xbdA382KU_1_SM_000006_1_1_ _ .jpg\r\n
*get* 550 1536225052000_G45\xe9\xab\x98\xe9\x80\x9f479KM 100M\xe7\x94\xb1\xe4\xb8\x9c\xe5\x90\x91\xe8\xa5\xbf_ _0_0_\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d_02_\xe8\xbe\xbdA382KU_1_SM_000006_1_1_ _ .jpg: Cannot create file.\r\n
*resp* 550 1536225052000_G45\xe9\xab\x98\xe9\x80\x9f479KM 100M\xe7\x94\xb1\xe4\xb8\x9c\xe5\x90\x91\xe8\xa5\xbf_ _0_0_\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d_02_\xe8\xbe\xbdA382KU_1_SM_000006_1_1_ _ .jpg: Cannot create file.

ftp上傳文件封裝