Python上传失败(只上传了一半的文件)

时间:2014-03-10 09:35:19

标签: python urllib2 urllib artifactory httplib

我有一个Python上传器功能失败,因为它只上传文件的一部分(用二进制文件和文本文件测试)。

def upload_u(logger, encoded_credentials, local_file_path, remote_path):
  logger.info("Uploading %s to %s...", local_file_path, remote_path)
    url = "https://my-artifactory-repo-site.org/artifactory/my-artifactory-repository/%s" % remote_path
    authentication_header = "Basic %s" % encoded_credentials

    logger.info("Calling %s", url)
    f = open(local_file_path, READ_FILE_AS_BINARY)
    request = urllib2.Request(url, data=f.read())
    f.close()
    request.get_method = lambda: PUT_REQUEST
    request.add_header(AUTHORIZATION_HEADER_KEY, authentication_header)
    request.add_header('Content-Length', os.path.getsize(local_file_path))
    request.add_header('Content-Type', 'application/octet-stream')
    print request.headers

    response = None
    try:
        response = urllib2.urlopen(request)
        print response.read()
        response.close()
        logger.info("Upload finished. (Status Code: HTTP %d)", response.getcode())
    except urllib2.URLError, e:
        if e.code not in OK_STATUS_CODES:
            logger.error("Upload failed.\nStatus Code: HTTP %d\nReason: %s", e.code, "TODO")
            raise UploaderError("Upload failed with status code %d" % e.code)
        else:
            logger.info("Upload finished. (Status Code: HTTP %d)", e.code)

这是 urllib2 ,但我也尝试过使用urllib和httplib。它只适用于请求,但我无法使用该库,因为我必须使用内置的。

我也尝试过:

  • 不以二进制模式打开文件

  • 传递类文件对象而不是内容

1 个答案:

答案 0 :(得分:0)

除非READ_FILE_AS_BINARYPUT_REQUESTAUTHORIZATION_HEADER_KEY有一些不寻常的值;你的代码应该按原样运行,问题出在其他地方。

要进行调试,请尝试使用可以上传不适合内存的大型文件的代码:

#!/usr/bin/env python
import mmap
import sys
from contextlib import closing
from urllib2 import Request, urlopen

url, file_to_upload = sys.argv[1:3]
credentials = 'dXNlcjpwYXNzd29yZA=='

with open(file_to_upload,'rb') as file:
    with closing(mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)) as mm:
        request = Request(url, mm)
        request.get_method = lambda: "PUT"
        request.add_header("Authorization", "Basic " + credentials)
        request.add_header("Content-type", "application/octet-stream")
        # Content-Length is set automatically
        with closing(urlopen(request)) as response:
            print(response.info())

如果它还只上传了文件的一半,则问题不在您的客户端Python代码中。