python upload file and form
来源:互联网 发布:怎么添加usb端口 编辑:程序博客网 时间:2024/05/29 03:06
#!/usr/bin/python# demo from here: http://pymotw.com/2/urllib2/index.html#uploading-filesimport itertoolsimport mimetoolsimport mimetypesfrom cStringIO import StringIOimport urllibimport urllib2class MultiPartForm(object): """Accumulate the data to be used when posting a form.""" def __init__(self): self.form_fields = [] self.files = [] self.boundary = mimetools.choose_boundary() return def get_content_type(self): return 'multipart/form-data; boundary=%s' % self.boundary def add_field(self, name, value): """Add a simple field to the form data.""" self.form_fields.append((name, value)) return def add_file(self, fieldname, filename, fileHandle, mimetype=None): """Add a file to be uploaded.""" body = fileHandle.read() if mimetype is None: mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' self.files.append((fieldname, filename, mimetype, body)) return def __str__(self): """Return a string representing the form data, including attached files.""" # Build a list of lists, each containing "lines" of the # request. Each part is separated by a boundary string. # Once the list is built, return a string where each # line is separated by '\r\n'. parts = [] part_boundary = '--' + self.boundary # Add the form fields parts.extend( [ part_boundary, 'Content-Disposition: form-data; name="%s"' % name, '', value, ] for name, value in self.form_fields ) # Add the files to upload parts.extend( [ part_boundary, 'Content-Disposition: file; name="%s"; filename="%s"' % \ (field_name, filename), 'Content-Type: %s' % content_type, '', body, ] for field_name, filename, content_type, body in self.files ) # Flatten the list and add closing boundary marker, # then return CR+LF separated data flattened = list(itertools.chain(*parts)) flattened.append('--' + self.boundary + '--') flattened.append('') return '\r\n'.join(flattened)if __name__ == '__main__': # Create the form with simple fields form = MultiPartForm() form.add_field('userID', '15844444444') form.add_field('username', '15844444444') form.add_field('loginToken', 'E02518FFBC69BD82C8A19048CF17A99E') form.add_field('filetype', 'erp') # Add a fake file form.add_file('file', 'data.json', fileHandle=StringIO('Python developer and blogger.')) # Build the request request = urllib2.Request('127.0.0.1:8080') request.add_header('User-agent', 'PyMOTW (http://www.doughellmann.com/PyMOTW/)') body = str(form) request.add_header('Content-type', form.get_content_type()) request.add_header('Content-length', len(body)) request.add_data(body) print print 'OUTGOING DATA:' print request.get_data() print print 'SERVER RESPONSE:' print urllib2.urlopen(request).read()
0 0
- python upload file and form
- Python and Golang upload images
- Upload and Download File using Java
- upload large file use html and servlet
- SAP UI5 upload file and oData response
- Useful Code of the Day: Multipart Form File Upload
- Handling Form-based File Upload with Java Servlet or JSP
- axis2 form upload file 异常 Invalid UTF-8 middle byte
- Form-based File Upload in HTML(RFC-1867)
- Upload file to servlet without using HTML form
- Use DropzondJS and Spring to upload and download file
- upload file
- Upload file
- upload file
- File Upload
- ASP.NET Interfaces: Internet File Select and Upload Dialog
- Struts 2 File Upload and Save Tutorial with Example
- Spring MVC + Mybatis+MySQL File Upload and Download Example
- Cudnn安装详细步骤
- python itertools 模块完全掌握(1)
- #708 – 将文件拖入到WPF应用程序中(Dragging a File Into a WPF Application)
- HDU
- exp和expdp怎么保证数据的一致性?
- python upload file and form
- springmvc常用注解标签详解
- popupWindow 嵌套scrollview异常显示的原因.
- 半闲居士视觉SLAM十四讲笔记(4)李群与李代数
- AndroidStudio非法字符: '\ufeff'解决
- 运算符的优先级
- Codeforces 404C Restore Graph 构造
- 《C语言及程序设计》教学视频 <18 链表中结点的插入和删除> 示例代码
- jquery自己写插件