Windows上的Requests和requeststolbelt

2024-10-06 11:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

这段代码在unix服务器之间运行良好,但是当客户机是Windows时,服务器总是抛出UnicodeDecodeError,如下面的堆栈跟踪所示。我不明白为什么请求试图解码文件对象。似乎Windows客户机上的temp_file采用了不同的形式,因此当请求到达服务器时,编码也不同。任何线索都会很棒。你知道吗

# client-side (Windows) - some code omitted for brevity

temp_file = TemporaryFile()
temp_file.write(file.read(chunk_size))
temp_file.seek(0)
payload = MultipartEncoder(
    {'uploadedFile': (path, temp_file, 'application/octet-stream'),
    'position': str(position), 'chunk_size': str(chunk_size),
    'chunk_number': str(chunk_number)})
r = requests.post(url, data=payload, headers={'Content-Type': payload.content_type})
temp_file.close()


# server-side (Unix)

@view_config(route_name='remote.agent_upload', renderer='json')
def remote_agent_upload(request):
    r = request.response
    uploadedFile = request.POST['uploadedFile']  # ERROR HERE
    chunk_size = request.POST['chunk_size']
    chunk_number = request.POST['chunk_number']
    position = request.POST['position']
    fs = uploadedFile.file
    filename = uploadedFile.filename
    fs.seek(0)
    path = os.path.join(agent.root, os.path.basename(filename))
    # remove the file if it exists
    if chunk_number == '0' and os.path.isfile(path):
        os.remove(path)
    f = open(path, 'a+b')
    f.seek(int(position))
    f.write(fs.read())
    fs.close()
    f.close()
return r

# relevant section of traceback
Traceback (most recent call last):

  File "/Volumes/Extra/Repos/env/lib/python2.7/site-packages/pyramid/config/views.py", line 501, in _requestonly_view
    response = view(request)
  File "/Volumes/Extra/Repos/bang/bang/remote_api.py", line 393, in remote_agent_upload
    uploadedFile = request.POST['uploadedFile']
  File "/Volumes/Extra/Repos/env/lib/python2.7/site-packages/webob/request.py", line 807, in POST
    vars = MultiDict.from_fieldstorage(fs)
  File "/Volumes/Extra/Repos/env/lib/python2.7/site-packages/webob/multidict.py", line 92, in from_fieldstorage
    obj.add(field.name, decode(value))
  File "/Volumes/Extra/Repos/env/lib/python2.7/site-packages/webob/multidict.py", line 78, in <lambda>
    decode = lambda b: b.decode(charset)
  File "/Volumes/Extra/Repos/env/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xba in position 3: invalid start byte

Tags: pathinpyrequestlinepositionpostextra
1条回答
网友
1楼 · 发布于 2024-10-06 11:28:35

Pyramid正试图将您的字段视为常规的非文件POST字段,这意味着它希望对字段值进行解码。因为没有文件名,或者文件名是空的,所以它就这样对待它。你知道吗

三重检查你上传的path变量。确保它是基名称(不允许使用目录名),并且不为空:

payload = MultipartEncoder(
    {'uploadedFile': (os.path.basename(path), temp_file, 'application/octet-stream'),
    'position': str(position), 'chunk_size': str(chunk_size),
    'chunk_number': str(chunk_number)})

相关问题 更多 >