• ftp上传文件封装


    业务需求,频繁传递图片到对方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
    '
    *get* '250 Directory successfully changed.
    '
    *resp* '250 Directory successfully changed.'
    *cmd* 'TYPE I'
    *put* 'TYPE I
    '
    *get* '200 Switching to Binary mode.
    '
    *resp* '200 Switching to Binary mode.'
    *cmd* 'PORT 10,10,4,48,237,115'
    *put* 'PORT 10,10,4,48,237,115
    '
    *get* '200 PORT command successful. Consider using PASV.
    '
    *resp* '200 PORT command successful. Consider using PASV.'
    *cmd* 'STOR 1535340773614_xe4xb8xb9xe4xb8x9c_ _0_0_xe6x89x93xe7x94xb5xe8xafx9d_02_xe9x84x82AW0613_1_SM_000006_1_1_ _ .jpg'
    *put* 'STOR 1535340773614_xe4xb8xb9xe4xb8x9c_ _0_0_xe6x89x93xe7x94xb5xe8xafx9d_02_xe9x84x82AW0613_1_SM_000006_1_1_ _ .jpg
    '
    *get* '150 Ok to send data.
    '
    *resp* '150 Ok to send data.'
    *get* '226 Transfer complete.
    '
    *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_大广高速623KM+600M 由东向西_ _0_0_未系安全带_02_辽ALN838_1_SM_000006_1_2_ _ .jpg: Cannot create file.
    
    # ftp debug
    *cmd* 'CWD xe6x89x93xe7x94xb5xe8xafx9d'
    *put* 'CWD xe6x89x93xe7x94xb5xe8xafx9d
    '
    *get* '250 Directory changed to /xe6x89x93xe7x94xb5xe8xafx9d\
    '
    *resp* '250 Directory changed to /xe6x89x93xe7x94xb5xe8xafx9d\'
    *cmd* 'TYPE I'
    *put* 'TYPE I
    '
    *get* '200 Type set to I.
    '
    *resp* '200 Type set to I.'
    *cmd* 'PORT 10,102,167,241,161,167'
    *put* 'PORT 10,102,167,241,161,167
    '
    *get* '200 PORT Command successful.
    '
    *resp* '200 PORT Command successful.'
    *cmd* 'STOR 1536225052000_G45xe9xabx98xe9x80x9f479KM 100Mxe7x94xb1xe4xb8x9cxe5x90x91xe8xa5xbf_ _0_0_xe6x89x93xe7x94xb5xe8xafx9d_02_xe8xbexbdA382KU_1_SM_000006_1_1_ _ .jpg'
    *put* 'STOR 1536225052000_G45xe9xabx98xe9x80x9f479KM 100Mxe7x94xb1xe4xb8x9cxe5x90x91xe8xa5xbf_ _0_0_xe6x89x93xe7x94xb5xe8xafx9d_02_xe8xbexbdA382KU_1_SM_000006_1_1_ _ .jpg
    '
    *get* '550 1536225052000_G45xe9xabx98xe9x80x9f479KM 100Mxe7x94xb1xe4xb8x9cxe5x90x91xe8xa5xbf_ _0_0_xe6x89x93xe7x94xb5xe8xafx9d_02_xe8xbexbdA382KU_1_SM_000006_1_1_ _ .jpg: Cannot create file.
    '
    *resp* '550 1536225052000_G45xe9xabx98xe9x80x9f479KM 100Mxe7x94xb1xe4xb8x9cxe5x90x91xe8xa5xbf_ _0_0_xe6x89x93xe7x94xb5xe8xafx9d_02_xe8xbexbdA382KU_1_SM_000006_1_1_ _ .jpg: Cannot create file.'

  • 相关阅读:
    CSS同时选择器
    create-react-app之proxy
    mysql limit语句
    给tbody加垂直滚动条的具体思路
    MySql数据类型范围
    block、inline、inline-block
    javascript sourcemap
    session of express
    React中innerHTML的坑
    box-sizing
  • 原文地址:https://www.cnblogs.com/elephanyu/p/python.html
Copyright © 2020-2023  润新知