2

i need to upload a image file, and i use MultipartPostHandler.py as people suggest. but still doesn't work. here is my code:

params = {"upload", open("12345.jpg", "rb")} // in 'rb'
opener = urllib2.build_opener(MultipartPostHandler)
res = opener.open(url, params)

and here is the code in MultipartPostHander:

def multipart_encode(vars, files, boundary = None, buffer = None):
    if boundary is None:
        boundary = mimetools.choose_boundary()
    if buffer is None:
        buffer = ''
    for(key, value) in vars:
        buffer += '--%s\r\n' % boundary
        buffer += 'Content-Disposition: form-data; name="%s"' % key
        buffer += '\r\n\r\n' + value + '\r\n'
    for(key, fd) in files:
        file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
        filename = fd.name.split('/')[-1]
        contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
        buffer += '--%s\r\n' % boundary
        buffer += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename)
        buffer += 'Content-Type: %s\r\n' % contenttype
        # buffer += 'Content-Length: %s\r\n' % file_size
        fd.seek(0)
        buffer += '\r\n' + fd.read() + '\r\n'
    buffer += '--%s--\r\n\r\n' % boundary
    return boundary, buffer
multipart_encode = Callable(multipart_encode)

https_request = http_request

and error appears at:

buffer += '\r\n' + fd.read() + '\r\n'

error is:

'ascii' codec can't decode byte 0xff in position 2: ordinal not in range(128)

this problem scrowed me a lot , please help me out! thx.

3
  • It's trying to append binary data to a string, but Python is trying to interpret that binary data as a string, and only allows valid ASCII characters in strings (bytes with values 0-127). Unfortunately I don't know how to append binary data to a string in Python, or I would have written an answer instead of a comment. Commented Oct 17, 2011 at 14:34
  • base64 encoding allows for a printable representation of binary data. I'm not sure about the standard, but it couldn't hurt to try an encoding such as that. Commented Oct 17, 2011 at 14:55
  • I'm not sure if this will work, but maybe you could use bytearray instead of str as a buffer. Commented Oct 17, 2011 at 14:57

2 Answers 2

1

I have used Doug Hellman's MultiPartForm class from his blog to upload files to our mail server sucessfully: http://pymotw.com/2/urllib2/index.html#module-urllib2

Hopefully you can use that as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Weird. I can upload any file I want with that script.
0

i think this can save me. aha.

# convert every byte of data to the corresponding 2-digit hexadecimal

enter code herehex_str = str(binascii.hexlify(data))`

# now create a list of 2-digit hexadecimals

hex_list = []

bin_list = []

for ix in range(2, len(hex_str)-1, 2):

    hex = hex_str[ix]+hex_str[ix+1]

    hex_list.append(hex)

    bin_list.append(bin(int(hex, 16))[2:])

#print(bin_list)

bin_str = "".join(bin_list)

print(bin_str)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.