Where is the mimetools.choose_boundary function in Python3?

Currently, I want to use the following code snippet in Python3, but found that the mimetools.choose_boundary function mimetools.choose_boundary deprecated, how do I change the code and make it work?

 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 
+5
source share
1 answer

Well, I answer my question, since there is no other answer available.

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


1. What does boundary do in a multipart/form-data request?

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

These parts can be a field value (plain text) or load the contents of a file.

2. First we put the boundary line in the request header.

To declare that the request is accepted in the format mulitipart/form-data , we first select a special line called boundary and put it in the request header:

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

When we see that here we select the FORM-BOUNDARY boundary line, we can select any desired line.

In most cases, we can choose a long, random string to prevent a collision.

3. Use the selected border in the request body.

In the request body (payload), we share data with a boundary delimiter, 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-- 

Upon seeing this, we will start one part of the form with a separator, after boundary after one character -- .

Then in this part of the form we export the title to declare the type of content and the name of this field.

Then one blank line is required.

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

After all parts of the form, we completed the request body with a separator, with a boundary between two characters -- .

4. So, what does mimetools.choose_boundary ??

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

Format:

 'hostipaddr.uid.pid.timestamp.random' 

Just so simple.

If we insist on getting the same result,

  • we ourselves can write the functionality.
  • Or call the email.generator function of the _make_boundary() module.

But really, to make it work, you don’t have to do it, just create a random string to replace it!

+10
source

Source: https://habr.com/ru/post/1207560/


All Articles