ftp上傳檔案封裝

來源:互聯網
上載者:User

標籤:特殊字元   imp   sha   dir   switch   __name__   col   中文目錄   class   

業務需求,頻繁傳遞圖片到對方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=315             )16             ftp.login(17                 user=‘xxx‘,18                 passwd=‘xxx‘19             )20             return ftp21         except Exception:22             lasterr = traceback.format_exc()23     print ‘error except when ftp connect, err: %s‘ % lasterr)24     return None25 26 def ftpCwd(ftp=None, path=‘/‘):27     try:28         ftp.cwd(path)29         return True30     except Exception:31         try:32             ftp.mkd(path)33             ftp.cwd(path)34             return True35         except ftplib.error_perm:36             print ‘error except when ftp cwd, err: %s‘ % traceback.format_exc()37     return False38 39 def ftpUpfile(ftp=None, srcpath=None, dstfile=None):40     bufsize = 102441     try:42         with open(srcpath, ‘rb‘) as fp:43             cmd = ‘STOR ‘ + dstfile44             ftp.storbinary(cmd, fp, bufsize)45             ftp.close()46             return True47     except Exception:48         print ‘error except when ftp put file,err: %s‘ % traceback.format_exc()49     return False50 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, resperror_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上傳檔案封裝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.