FTP Upload file encapsulation

Source: Internet
Author: User
Tags ftp upload file

Business requirements, the frequent transfer of pictures to the other FTP server, with this code can stream upload files.

Upload Code

1 #Coding:utf-82 ImportFtplib3 ImportLogging4 ImportTraceback5 6 defFTPLogin ():7LastErr =None8      forIinchRange (3):9         Try:TenFTP =Ftplib. FTP () One Ftp.connect ( Ahost='xx.xx.xx.xx', -Port=21, -Timeout=3 the             ) - Ftp.login ( -User='XXX', -Passwd='XXX' +             ) -             returnFTP +         exceptException: ALastErr =Traceback.format_exc () at     Print 'error except when FTP Connect, err:%s'%LastErr) -     returnNone -  - defFTPCWD (Ftp=none, path='/'): -     Try: - ftp.cwd (path) in         returnTrue -     exceptException: to         Try: + FTP.MKD (path) - ftp.cwd (path) the             returnTrue *         exceptFtplib.error_perm: $             Print 'error except when FTP CWD, err:%s'%Traceback.format_exc ()Panax Notoginseng     returnFalse -  the defFtpupfile (Ftp=none, Srcpath=none, dstfile=None): +BufSize = 1024 A     Try: theWith open (Srcpath,'RB') as FP: +cmd ='STOR'+Dstfile - ftp.storbinary (cmd, FP, bufsize) $ ftp.close () $             returnTrue -     exceptException: -         Print 'error except when FTP put File,err:%s'%Traceback.format_exc () the     returnFalse - Wuyi if __name__=='__main__': theFile ='a.txt' -FTP =FTPLogin () WuFtp.set_debuglevel (2) - FTP.SET_PASV (0) About     ifFtp is  notNone: $CWD = FTPCWD (ftp=ftp, path='AA') -         ifCWD: -ret = Ftpupfile (ftp=ftp, Srcpath=file, dstfile=file) -             ifret: ALogging.info ('put file Success') +Ftp.close ()

FTP Debug Content:

*cmd*'CWD AA'*put*'CWD aa\r\n'*get*'Directory successfully changed.\r\n'*resp*'Directory successfully changed.'*cmd*'TYPE I'*put*'TYPE i\r\n'*get*'switching to Binary mode.\r\n'*resp*'switching to Binary mode.'*cmd*'PORT 10,10,4,48,237,115'*put*'PORT 10,10,4,48,237,115\r\n'*get*'PORT Command successful. Consider using pasv.\r\n'*resp*'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*'Ok to send data.\r\n'*resp*'Ok to send data.'*get*'226 Transfer complete.\r\n'*resp*'226 Transfer complete.'

But there is a strange phenomenon:

Chinese catalog upload with special characters of Chinese pictures, code error but the image upload ftp server, very strange.

#python logs2018-09-06 17:49:05,794 pushprocess.py[line:76] Error ErrorexceptWhen the FTP put File,err:traceback (most recent call last): File"/home/seemmo/tongliao_push/seemmo/business/pushprocess.py", line 72,inchftpupfile ftp.storbinary (cmd, FP, bufsize) File"/home/seemmo/share/python/python-2.6/lib/ftplib.py", line 452,inchStorbinary Conn=self.transfercmd (cmd) File"/home/seemmo/share/python/python-2.6/lib/ftplib.py", Line 360,inchTransfercmdreturnself.ntransfercmd (cmd, rest) [0] File"/home/seemmo/share/python/python-2.6/lib/ftplib.py", Line 344,inchNtransfercmd resp=self.sendcmd (cmd) File"/home/seemmo/share/python/python-2.6/lib/ftplib.py", line 243,inchSendcmdreturnself.getresp () File"/home/seemmo/share/python/python-2.6/lib/ftplib.py", line 218,inchGetrespRaiseerror_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*'Directory changed to/\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d\\\r\n'*resp*'Directory changed to/\xe6\x89\x93\xe7\x94\xb5\xe8\xaf\x9d\\'*cmd*'TYPE I'*put*'TYPE i\r\n'*get*'$ Type set to i.\r\n'*resp*'The Type set to I.'*cmd*'PORT 10,102,167,241,161,167'*put*'PORT 10,102,167,241,161,167\r\n'*get*'PORT Command successful.\r\n'*resp*'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 Upload file encapsulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.