Python upload file

Source: Internet
Author: User

Last week, the product gave me a demand, is basically to do a back-end system, management game Competition landing page Data Update, the difficulty is to upload files to the CDN. Now that the experience is recorded, the next time there is a similar need to improve the efficiency of development.

I'm using a Web CDN, and I'm not using the Web-host SDK to upload files directly into Python-encapsulated forms.

Uploading a file using post requires encapsulating an HTTP message header:

Content-type:multipart/form-data; boundary=<boundary>  # boundary is a delimiter that uses <boundary> as the body divider

Content-length:xxxx

--<boundary> #开始必须使用 "--delimiter" as the first line of body, need to pay attention to--must not lose
Content-disposition:form-data; Name= "XXX"
/r/n # Note that there must be a blank line between name and value
Xxx_value # form xxx corresponding value values
Content-dispositon:form-data; Name= ' file '; Filename= ' Abc.png '
Content-type:image/png
/r/n
"Binary data for PNG"
--<boundary>-- #body体结束标记
/r/n



LIMIT ='----------limit_of_the_file_ew_$'CRLF='\ r \ n'L= [] for(Key, value)inchFields:l.append ('--'+LIMIT) L.append ('content-disposition:form-data; name= "%s"'%key) L.append ("') L.append (value) for(key, filename, value)inchFiles:l.append ('--'+LIMIT) L.append ('content-disposition:form-data; name= "%s"; filename= "%s "'%(key, filename)) L.append ('Content-type:%s'%get_content_type (filename)) L.append ("') L.append (value) l.append ('--'+ LIMIT +'--') L.append ("') Body= Crlf.join (L)

Python upload file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.