本文介绍了Python3中的mimetools.choose_boundary函数在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前想在Python3中使用以下代码,但是发现不推荐使用函数mimetools.choose_boundary,如何更改代码并使之起作用?

I currently want to use the following piece of code in Python3, but found the function mimetools.choose_boundary to be deprecated, how to change the code and make it works?

import re
from urllib.request import urlopen, Request
import os
import mimetypes
import mimetools

def get_content_type(filepath):
    return mimetypes.guess_type(filepath)[0] or 'application/octet-stream'

def encode_multipart_formdata(fields, files=[]):
    """
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filepath) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
    """
    BOUNDARY = mimetools.choose_boundary()
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filepath) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, os.path.basename(filepath)))
        L.append('Content-Type: %s' % get_content_type(filepath))
        L.append('')
        L.append(open(filepath, 'rb').read())
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body

推荐答案

好吧,我回答了我的问题,因为这里没有其他可用的答案.

Well, I answer my question since no other available answer here.

是的,我最终得到了结果,有关我在该问题上的工作的更多信息,下面的信息可能会有所帮助.

Yes, I got the result finally, for more info about my work around the question, the below information may help.

实际上,要分离数据的不同部分就是这样的请求,我们使用分隔符,这里我们称为boundary,以划分表单数据.

In fact, to separate the different parts of data is such a request, we use a separator, here we call boundary, to divide the form data.

这些部分可能是字段值(纯文本),或者是上载文件内容.

These parts may be field value (plain text), or uploading file contents.

要声明请求被接受为mulitipart/form-data格式,我们首先选择一个名为boundary的特殊字符串,并将其放在请求标头中:

To claim a request to be accepted as a mulitipart/form-data format, we first choose a special string, called boundary, and put it in the request header:

Content-Type: multipart/form-data; boundary=FORM-BOUNDARY

看到我们在这里选择边界字符串为FORM-BOUNDARY,实际上我们可以选择我们想要的任何字符串.

Seeing that we choose the boundary string to be FORM-BOUNDARY here, in fact we can choose any string we want.

大多数时候,我们可能会选择一个长而随机的字符串以防止碰撞.

Most time we may choose a long, randomly string, to prevent collision.

在请求正文(有效负载)中,我们用boundary分隔符分隔数据,例如:

In the request body (payload), we separate the data with the boundary separator, for example:

--FORM-BOUNDARY
Content-Disposition: form-data; name="template"; filename=".xls"
Content-Type: application/vnd.ms-excel

A654ADE5^%^#%@%$@ (BINARY DATA IN THIS SECTION)
--FORM-BOUNDARY
Content-Disposition: form-data; name="username"

admin
--FORM-BOUNDARY
Content-Disposition: form-data; name="password"

admin_password
--FORM-BOUNDARY--

看到这一点,我们用一个分隔符开始一个表单部分,在单个--符号后以boundary开头.

Seeing that, we start one form-part with a separator, with the boundary after a single -- symbol.

然后在该表单部分中,导出标题以声明内容类型和该发布字段的名称.

Then in that form-part, we export the header to claim the content type and the name of that posted field.

然后需要一个空白行.

然后我们导出该表单部分的值(数据).

Then we export the value(data) of that form-part.

在所有表单部分之后,我们用一个分隔符结束请求正文,在两个--符号之间使用boundary.

After all form-parts, we ended the request body with a separator, with the boundary between two -- symbol.

实际上,此函数(自py3起已弃用)会生成具有指定格式的随机边界,请参见: https://docs.python.org/2.7/library/mimetools.html?highlight=choose_boundary#mimetools.choose_boundary

In fact, this function (deprecated since py3) generate a random boundary, with a specified format, see: https://docs.python.org/2.7/library/mimetools.html?highlight=choose_boundary#mimetools.choose_boundary

格式为:

'hostipaddr.uid.pid.timestamp.random'

就这么简单.

如果我们坚持要获得相同的结果,

If we insist on getting the same result,

  1. 我们可以自己编写功能.
  2. 或调用email.generator模块的_make_boundary()函数.
  1. we can write the functional by ourselves.
  2. Or call the email.generator module's _make_boundary() function.

这篇关于Python3中的mimetools.choose_boundary函数在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 02:44
查看更多