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/13 03:00] – [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 === |
| + | Post with httplib< | ||
| import httplib, mimetypes | import httplib, mimetypes | ||
| Line 421: | Line 422: | ||
| def get_content_type(filename): | def get_content_type(filename): | ||
| return mimetypes.guess_type(filename)[0] or ' | return mimetypes.guess_type(filename)[0] or ' | ||
| + | </ | ||
| + | === 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:< | ||
| + | import MultipartPostHandler, | ||
| + | |||
| + | cookies = cookielib.CookieJar() | ||
| + | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), | ||
| + | MultipartPostHandler.MultipartPostHandler) | ||
| + | params = { " | ||
| + | " | ||
| + | opener.open(" | ||
| </ | </ | ||
| ===== twisted internet ===== | ===== twisted internet ===== | ||
python/internet.1407898834.txt.gz · Last modified: (external edit)
