| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 743 人关注过本帖
标题:pycurl+ftp上传数据,但上传上去的大小总为0
只看楼主 加入收藏
zhou31146001
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:303
专家分:131
注 册:2012-11-28
结帖率:93.94%
收藏
 问题点数:0 回复次数:0 
pycurl+ftp上传数据,但上传上去的大小总为0
各位大侠好。
下述代码基于 pycurl + ftp协议 向ftp服务器上传数据,但总是出现下述问题:
Uploaded unaligned file size (0 out of 10261268 bytes) 即可以上传上去,但大小都是 0 ;

请问各位大侠,如何解决,谢谢!
下述为代码


import time
import os
import ftplib
import ssl
import socket
import pycurl, StringIO
from os.path import getsize
import ftplib
from ftplib import FTP
from ftps import FTPS
import pytest

from mock import patch
from six.moves import range
from six import BytesIO
import logging
LOGGER = logging.getLogger('ftps')

class FTPS11(object):

    def __init__(self, url, connect_timeout=5, max_retries=5):
        assert url.startswith('ftps://'), 'Expected URL scheme is ftps'
        self.base_url = url
        self.connect_timeout = connect_timeout
        self.max_retries = max_retries
        self.client = pycurl.Curl()
        self.reset()
    def reset(self):
        self.client.reset()
    #self.client.setopt(pycurl.USE_SSL, USESSL_ALL)
        self.client.setopt(pycurl.SSL_VERIFYPEER, False)
        self.client.setopt(pycurl.SSL_VERIFYHOST, False)
        self.client.setopt(pycurl.CONNECTTIMEOUT, self.connect_timeout)

    def perform(self):
        """Perform operation with retries."""
        retries = 0
        while retries < self.max_retries:
            try:
                self.client.perform()
                LOGGER.debug('Operation performed successfully')
                return
            except pycurl.error as exc:
                LOGGER.warning(exc)
                LOGGER.debug('Retrying (%d)...', retries)
                retries += 1
        LOGGER.error('Failed to perform operation')

    def list(self, remote_dir=None):
        if remote_dir is None:
            # List root directory by default
            remote_dir = ''
        elif not remote_dir.endswith('/'):
            # Make sure that directory ends with a forward slash character
            remote_dir += '/'
        url = '/'.join((self.base_url, remote_dir))
        self.client.setopt(pycurl.URL, url)
        #self.client.setopt(pycurl.USERPWD, 'ydrobot:123qweasd')
        #self.client.setopt(pycurl.USERPWD, 'ftp_test:1')
        output_buffer = BytesIO()
        self.client.setopt(pycurl.WRITEDATA, output_buffer)
        LOGGER.debug('Listing directory: %s', url)
        self.perform()
        self.reset()
        output = output_buffer.getvalue().decode('utf-8')
        files = [
            line.split()[-1]
            for line in output.split('\n')
            if line
        ]

        return files

    def download(self, remote_filename, local_filename):
        url = '/'.join((self.base_url, remote_filename))
        self.client.setopt(pycurl.URL, url)

        with open(local_filename, 'wb') as output_file:
            self.client.setopt(pycurl.WRITEDATA, output_file)
            LOGGER.debug('Downloading file: %s -> %s', url, local_filename)
            self.perform()
        self.reset()

    def upload(self, local_filename, remote_filename):
        #url = '/'.join((self.base_url, remote_filename))
        url = os.path.join(self.base_url, remote_filename)
        self.client.setopt(pycurl.URL, url)
        print ('55555555555',self.base_url,url)
        self.client.setopt(pycurl.URL, url)
        #self.client.setopt(pycurl.USERPWD, 'ydrobot:123qweasd')
        #self.client.setopt(pycurl.USERPWD, 'ftp_test:1')
        #self.client.enterLocalPassiveMode()
        self.client.setopt(pycurl.VERBOSE, 1)
        with open(local_filename, 'rb') as input_file:   ### with open(local_filename, 'rb') as input_file 相当于  input_file = open(local_filename, 'rb')   
            self.client.setopt(pycurl.UPLOAD, True)
            print ('333333333333333333333333333333333333333333',os.path.getsize(local_filename),input_file,local_filename)
            self.client.setopt(pycurl.READDATA, input_file)
            self.client.setopt(
                pycurl.INFILESIZE_LARGE,
                os.path.getsize(local_filename),
            )
            LOGGER.debug('Uploading file: %s -> %s', local_filename, url)
            self.perform()
            #os.remove(local_filename)
        self.reset()
        return url


if __name__ == '__main__':

    ftpss = FTPS11("ftps://")
    address = ''
    #mulmsg.equipimage = np.array(cv2.imencode('.jpg', swordImg)[1]).tostring()
    timename = time.strftime("%Y%m%d%H%M%S",time.localtime())+'.jpg'
    #baseaddress = filePath + '/filehandling/' + timename
    baseaddress = "/home/yd/11.tar.xz"
    #ftpdest = '192.168.1.35:21/2022/3/28/2064/CCD/140000/1/' + timename   ### 不能写为 '192.168.1.35:21/home/ydrobot/2022/3/28/2064/CCD/140000/1/'  这样出错
    ftpdest = 'ftp_test:1@192.168.1.237:21/11.tar.xz'  
    print ("1111111111111111111111111111111111111111111111")
    address = ftpss.upload(baseaddress,ftpdest)
    print ('22222222222222222222222222222222222222222222222')
搜索更多相关主题的帖子: 上传 url def import client 
2022-03-31 10:12
快速回复:pycurl+ftp上传数据,但上传上去的大小总为0
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020483 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved