python:internet
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python:internet [2014/08/16 09:56] – [post multipart form data] admin | python:internet [2022/10/29 16:15] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 372: | Line 372: | ||
PHPSESSID 01eg0u7uf5bm3r01h6pnrv3q33 | PHPSESSID 01eg0u7uf5bm3r01h6pnrv3q33 | ||
</ | </ | ||
- | ==== post multipart form data ==== | + | ==== Post multipart form data ==== |
- | | + | === post with encode_multipart_formdata === |
- | * Install:< | + | Post with httplib< |
- | pip install MultipartPostHandler | + | |
- | </ | + | |
- | * example:<code python> | + | |
import httplib, mimetypes | import httplib, mimetypes | ||
Line 426: | Line 423: | ||
return mimetypes.guess_type(filename)[0] or ' | return mimetypes.guess_type(filename)[0] or ' | ||
</ | </ | ||
- | * Post using MultipartPostHandler< | + | === fix encode_multipart_formdata for posting binary file === |
+ | <code python> | ||
+ | def encode_multipart_formdata(fields, | ||
+ | """ | ||
+ | fields is a sequence of (name, value) elements for regular form fields. | ||
+ | files is a sequence of (name, filename, value) elements for data to be uploaded as files | ||
+ | Return (content_type, | ||
+ | """ | ||
+ | |||
+ | buf = StringIO() | ||
+ | boundary = mimetools.choose_boundary() | ||
+ | for (key, value) in fields: | ||
+ | buf.write(' | ||
+ | buf.write(' | ||
+ | buf.write(' | ||
+ | for (key, filename, value) in files: | ||
+ | contenttype = mimetypes.guess_type(filename)[0] or ' | ||
+ | buf.write(' | ||
+ | buf.write(' | ||
+ | buf.write(' | ||
+ | buf.write(' | ||
+ | buf.write(' | ||
+ | buf = buf.getvalue() | ||
+ | content_type = ' | ||
+ | return content_type, | ||
+ | </ | ||
+ | === Post using MultipartPostHandler | ||
+ | * Install:< | ||
+ | pip install MultipartPostHandler | ||
+ | </ | ||
+ | * example:<code python> | ||
import MultipartPostHandler, | import MultipartPostHandler, | ||
python/internet.1408182985.txt.gz · Last modified: 2022/10/29 16:15 (external edit)