Skip to content

Instantly share code, notes, and snippets.

@yurtaev
Last active October 22, 2020 21:14
Show Gist options
  • Save yurtaev/294a5fbd78016e5d7456 to your computer and use it in GitHub Desktop.
Save yurtaev/294a5fbd78016e5d7456 to your computer and use it in GitHub Desktop.
import httplib
import mimetypes
import base64
def post_multipart(host, selector, fields, files, headers):
"""
Post fields and files to an http host as multipart/form-data.
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 the server's response page.
"""
content_type, body = encode_multipart_formdata(fields, files)
h = httplib.HTTP(host)
h.putrequest('POST', selector)
h.putheader('content-type', content_type)
h.putheader('content-length', str(len(body)))
if headers is not None:
for key, value in headers.iteritems():
h.putheader(key, value)
h.endheaders()
h.send(body)
errcode, errmsg, headers = h.getreply()
return h.file.read()
def encode_multipart_formdata(fields, files):
"""
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, body) ready for httplib.HTTP instance
"""
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = '\r\n'
L = []
for (key, value) in fields:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
#
# Settings
#
host = 'omaha-server-dev.elasticbeanstalk.com'
username = 'username'
password = 'password'
token = base64.b64encode('%s:%s' % (username, password))
headers = {
'Authorization': 'Basic %s' % token
}
#
# Upload Symbols File
#
path_to_symbols_file = '/data/BreakpadTestApp.sym'
url_symbols = '/api/symbols/'
with open(path_to_symbols_file, 'rb') as f:
file_content = f.read()
files = [
('file', 'BreakpadTestApp.sym', file_content),
]
print post_multipart(host, url_symbols, [], files, headers)
#
# Upload New Version
#
url_omaha = '/api/omaha/version/'
path_to_new_version_file = '/data/chromuim.exe'
with open('BreakpadTestApp.sym', 'rb') as f:
file_content = f.read()
files = [
('file', path_to_new_version_file, file_content),
]
fields = [
('app', '{8A76FC95-0086-4BCE-9517-DC09DDB5652F}'),
('channel', '1'),
('platform', '1'),
('version', '1.2.3.4'),
]
print post_multipart(host, url_omaha, fields, files, headers)
#
# Add Action for Omaha Version
#
url_action = '/api/action/'
fields = [
('version', '11'),
('event', '1'),
]
print post_multipart(host, url_action, fields, [], headers)
#
# Upload New Sparkle Version
#
url_sparkle = '/api/sparkle/version/'
path_to_new_version_file = '/data/chromuim.dmg'
with open('BreakpadTestApp.sym', 'rb') as f:
file_content = f.read()
files = [
('file', path_to_new_version_file, file_content),
]
fields = [
('app', '{8A76FC95-0086-4BCE-9517-DC09DDB5652F}'),
('channel', '1'),
('platform', '1'),
('version', '3.4'),
('short_version', '1.2.3.4'),
('dsa_signature', 'MCwCFFjHuSSd/QKCuIJsl7T2GDQd1NeZAhRqnZqXoFdpbfzyaE772N0TISwFzQ=='),
]
print post_multipart(host, url_sparkle, fields, files, headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment