Created
January 29, 2014 14:49
-
-
Save pierreant-p/8689576 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crafted with love by https://github.com/cambonf | |
from __future__ import print_function | |
from urllib.request import urlopen, Request | |
import mimetypes | |
import random | |
import string | |
# Encode form-data without external dependencies | |
# See: http://code.activestate.com/recipes/578668-encode-multipart-form-data-for-uploading-files-via/ | |
_BOUNDARY_CHARS = string.digits + string.ascii_letters | |
def encode_multipart(fields, files, boundary=None): | |
r"""Encode dict of form fields and dict of files as multipart/form-data. | |
Return tuple of (body_string, headers_dict). Each value in files is a dict | |
with required keys 'filename' and 'content', and optional 'mimetype' (if | |
not specified, tries to guess mime type or uses 'application/octet-stream'). | |
>>> body, headers = encode_multipart({'FIELD': 'VALUE'}, | |
... {'FILE': {'filename': 'F.TXT', 'content': 'CONTENT'}}, | |
... boundary='BOUNDARY') | |
>>> print('\n'.join(repr(l) for l in body.split('\r\n'))) | |
'--BOUNDARY' | |
'Content-Disposition: form-data; name="FIELD"' | |
'' | |
'VALUE' | |
'--BOUNDARY' | |
'Content-Disposition: form-data; name="FILE"; filename="F.TXT"' | |
'Content-Type: text/plain' | |
'' | |
'CONTENT' | |
'--BOUNDARY--' | |
'' | |
>>> print(sorted(headers.items())) | |
[('Content-Length', '193'), ('Content-Type', 'multipart/form-data; boundary=BOUNDARY')] | |
>>> len(body) | |
193 | |
""" | |
def escape_quote(s): | |
return s.replace('"', '\\"') | |
if boundary is None: | |
boundary = ''.join(random.choice(_BOUNDARY_CHARS) for i in range(30)) | |
lines = [] | |
for name, value in fields.items(): | |
lines.extend(( | |
'--{0}'.format(boundary), | |
'Content-Disposition: form-data; name="{0}"'.format(escape_quote(name)), | |
'', | |
str(value), | |
)) | |
for name, value in files.items(): | |
filename = value['filename'] | |
if 'mimetype' in value: | |
mimetype = value['mimetype'] | |
else: | |
mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' | |
lines.extend(( | |
'--{0}'.format(boundary), | |
'Content-Disposition: form-data; name="{0}"; filename="{1}"'.format( | |
escape_quote(name), escape_quote(filename)), | |
'Content-Type: {0}'.format(mimetype), | |
'', | |
value['content'], | |
)) | |
lines.extend(( | |
'--{0}--'.format(boundary), | |
'', | |
)) | |
body = '\r\n'.join(lines) | |
headers = { | |
'Content-Type': 'multipart/form-data; boundary={0}'.format(boundary), | |
'Content-Length': str(len(body)), | |
} | |
return (body, headers) | |
# upload function | |
def upload(): | |
path="./" | |
filename="airboat.obj" | |
description="Test of the api with a simple model" | |
token_api="YOUR TOKEN" | |
title="Airboat !" | |
tags="" | |
private=0 | |
password="" | |
url="https://api.sketchfab.com/v1/models" | |
data = { | |
'title': title , | |
'description': description, | |
'filenameModel': filename, | |
'tags': tags, | |
'token': token_api, | |
'private': private, | |
'password': password | |
} | |
files = { | |
"fileModel": { | |
"filename" : filename, | |
"content" : open(path+filename).read() | |
} | |
} | |
data, headers = encode_multipart(data, files) | |
request = Request(url, data=data.encode('utf-8'), headers=headers) | |
f = urlopen(request) | |
print(f.read()) | |
# run in the same thread | |
upload() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment