Python requests_toolbelt MultipartEncoder 文件名

MFB*_*MFB 4 python python-requests

使用 requests_toolbelt 以多部分形式上传大文件,我构建了一个方法,可以成功上传文件,但是我无法访问发布的文件名。如何访问服务器上的文件名?

# client-side
file = open('/Volumes/Extra/test/my_video.mpg', 'rb')
payload = MultipartEncoder({file.name: file})
r = requests.post(url, data=payload, headers={'Content-Type': 'application/octet-stream'})

# server-side
@view_config(route_name='remote.agent_upload', renderer='json')
def remote_agent_upload(request):
    r = request.response
    fs = request.body_file
    f = open('/Volumes/Extra/tests2/bar.mpg', 'wb')  # wish to use filename here
    f.write(fs.read())
    fs.close()
    f.close()
    return r
Run Code Online (Sandbox Code Playgroud)

Ray*_*rea 5

好的,看起来您正在使用文件名作为字段名。另外,您这样做的方式似乎整个帖子内容都被写入文件......这是期望的结果吗?在服务器端写入 mpg 文件后,您是否尝试过实际播放它们?

我目前没有可供测试的 HTTP 服务器,它会自动为我提供一个请求对象,但我假设该请求对象是一个 webob.Request 对象(至少看起来是这样,请更正)如果我错了就是我)

好吧,让我给你展示一下我的测试。(这适用于 python3.4,不确定您使用的是什么版本的 Python,但我认为它也应该适用于 Python 2.7 - 虽然未经测试)

此测试中的代码有点长,但有大量注释,可以帮助您理解我在每一步中所做的事情。希望它能让您更好地了解 HTTP 请求和响应如何通过您正在使用的工具在 Python 中工作

# My Imports
from requests_toolbelt import MultipartEncoder
from webob import Request
import io

# Create a buffer object that can be read by the MultipartEncoder class
# This works just like an open file object
file = io.BytesIO()

# The file content will be simple for my test.
# But you could just as easily have a multi-megabyte mpg file
# Write the contents to the file
file.write(b'test mpg content')

# Then seek to the beginning of the file so that the
# MultipartEncoder can read it from the beginning
file.seek(0)

# Create the payload
payload = MultipartEncoder(
    {

        # The name of the file upload field... Not the file name
        'uploadedFile': (

            # This would be the name of the file
            'This is my file.mpg',

            # The file handle that is ready to be read from
            file,

            # The content type of the file
            'application/octet-stream'
        )
    }
)

# To send the file, you would use the requests.post method
# But the content type is not application-octet-stream
# The content type is multipart/form-data; with a boundary string
# Without the proper header type, your server would not be able to
# figure out where the file begins and ends and would think the
# entire post content is the file, which it is not. The post content
# might even contain multiple files
# So, to send your file, you would use:
#
# response = requests.post(url, data=payload, headers={'Content-Type': payload.content_type})

# Instead of sending the payload to the server,
# I am just going to grab the output as it would be sent
# This is because I don't have a server, but I can easily
# re-create the object using this output
postData = payload.to_string()

# Create an input buffer object
# This will be read by our server (our webob.Request object)
inputBuffer = io.BytesIO()

# Write the post data to the input buffer so that the webob.Request object can read it
inputBuffer.write(postData)

# And, once again, seek to 0
inputBuffer.seek(0)

# Create an error buffer so that errors can be written to it if there are any
errorBuffer = io.BytesIO()

# Setup our wsgi environment just like the server would give us
environment = {
    'HTTP_HOST': 'localhost:80',
    'PATH_INFO': '/index.py',
    'QUERY_STRING': '',
    'REQUEST_METHOD': 'POST',
    'SCRIPT_NAME': '',
    'SERVER_NAME': 'localhost',
    'SERVER_PORT': '80',
    'SERVER_PROTOCOL': 'HTTP/1.0',
    'CONTENT_TYPE': payload.content_type,
    'wsgi.errors': errorBuffer,
    'wsgi.input': inputBuffer,
    'wsgi.multiprocess': False,
    'wsgi.multithread': False,
    'wsgi.run_once': False,
    'wsgi.url_scheme': 'http',
    'wsgi.version': (1, 0)
}

# Create our request object
# This is the same as your request object and should have all our info for reading
# the file content as well as the file name
request = Request(environment)

# At this point, the request object is the same as what you get on your server
# So, from this point on, you can use the following code to get
# your actual file content as well as your file name from the object

# Our uploaded file is in the POST. And the POST field name is 'uploadedFile'
# Grab our file so that it can be read
uploadedFile = request.POST['uploadedFile']

# To read our content, you can use uploadedFile.file.read()
print(uploadedFile.file.read())

# And to get the file name, you can use uploadedFile.filename
print(uploadedFile.filename)
Run Code Online (Sandbox Code Playgroud)

所以,我认为这个修改后的代码对你有用。(希望)同样,没有测试,因为我实际上没有服务器可以测试。而且,我不知道你的“请求”对象在服务器端是什么样的对象......好吧,这里是:

# client-side
import requests
file = open('/Volumes/Extra/test/my_video.mpg', 'rb')
payload = MultipartEncoder({'uploadedFile': (file.name, file, 'application/octet-stream')})
r = requests.post('http://somewhere/somefile.py', data=payload, headers={'Content-Type': payload.content_type})

# server-side
@view_config(route_name='remote.agent_upload', renderer='json')
def remote_agent_upload(request):

    # Write your actual file contents, not the post data which contains multi part boundary
    uploadedFile = request.POST['uploadedFile']
    fs = uploadedFile.file

    # The file name is insecure. What if the file name comes through as '../../../etc/passwd'
    # If you don't secure this, you've just wiped your /etc/passwd file and your server is toast
    # (assuming the web user has write permission to the /etc/passwd file
    # which it shouldn't, but just giving you a worst case scenario)
    fileName = uploadedFile.filename

    # Secure the fileName here... 
    # Make sure it doesn't have any slashes or double dots, or illegal characters, etc.
    # I'll leave that up to you

    # Write the file
    f = open('/Volumes/Extra/tests2/' + fileName, 'wb')
    f.write(fs.read())
Run Code Online (Sandbox Code Playgroud)