Skip to content

Instantly share code, notes, and snippets.

@AhnMo
Last active April 4, 2021 11:01
Show Gist options
  • Save AhnMo/be8cc21bf02c9e92247d74d460727ce0 to your computer and use it in GitHub Desktop.
Save AhnMo/be8cc21bf02c9e92247d74d460727ce0 to your computer and use it in GitHub Desktop.
python urllib2 file upload
import urllib2
import urllib
import itertools
import mimetools
import mimetypes
from cStringIO import StringIO
class 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)
url = "http://somewhere/upload.php"
headers = {
'User-Agent' : 'Mozilla/5.0',
'Cookie' : 'PHPSESSID=00000000000000000000000000'
}
def upload():
form = MultiPartForm()
form.add_field('subject', 'blahblahbalh')
form.add_file('file', 'a.pdf',
fileHandle=open('Dummy.pdf', 'rb')) # from file
# fileHandle=StringIO('Dummy')) # from string
body = str(form)
req = urllib2.Request(url, '', headers)
req.add_header('Content-Type', form.get_content_type())
req.add_header('Content-Length', len(body))
req.add_data(body)
return urllib2.urlopen(req).read()
upload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment