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