urllib2 - Python upload fails (only half of the file uploaded) -
i have python uploader function fails because uploads part of file (tested binaries , text files).
def upload_u(logger, encoded_credentials, local_file_path, remote_path): logger.info("uploading %s %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 status code %d" % e.code) else: logger.info("upload finished. (status code: http %d)", e.code)
this urllib2, have tried urllib , httplib. works requests not use library because have use built in one.
i have tried to:
open file not in binary mode
pass file-like object instead of content
unless read_file_as_binary
, put_request
, authorization_header_key
have unusual values; code should work , issue elsewhere.
for debugging, try code can upload large files not fit in memory:
#!/usr/bin/env python import mmap import sys contextlib import closing urllib2 import request, urlopen url, file_to_upload = sys.argv[1:3] credentials = 'dxnlcjpwyxnzd29yza==' open(file_to_upload,'rb') file: closing(mmap.mmap(file.fileno(), 0, access=mmap.access_read)) 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 set automatically closing(urlopen(request)) response: print(response.info())
if uploads half of file problem not in client python code.
Comments
Post a Comment