Skip to content

Instantly share code, notes, and snippets.

@horsey
Created October 23, 2014 20:29
Show Gist options
  • Select an option

  • Save horsey/d2e63828e66ebc879224 to your computer and use it in GitHub Desktop.

Select an option

Save horsey/d2e63828e66ebc879224 to your computer and use it in GitHub Desktop.
Disclaimer:
This piece was inspired by: http://code.activestate.com/recipes/146306/
def post_multipart(host, port, selector, fields, files, ownerid):
content_type, body = encode_multipart_formdata(fields, files)
h = httplib.HTTPConnection(host, port)
headers = {
'User-Agent': 'Build Cluster 1.0',
'Content-Type': content_type,
'X-Fcres-Requid' : ownerid
}
h.request('POST', selector, body, headers)
res = h.getresponse()
return res.status, res.reason, res.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 = u'\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('')
#value = value.encode ('base64')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
#body = CRLF.join(L)
body = StringIO()
for buf in L:
body.write (buf)
body.write (CRLF)
body = body.getvalue()
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
------
The exception:
2014-10-23 06:40:20,067 studio_fone-3664-601886655-1280-1098 ERROR Traceback (most recent call last):
2014-10-23 06:40:20,067 studio_fone-3664-601886655-1280-1098 ERROR File "/opt/beedi/beedi/requestbuild.py", line 308, in UploadApp -- (status, reason, content) = qzhttputils.posturl (url, gFCPort, fields, files, a_ownerid)
2014-10-23 06:40:20,068 studio_fone-3664-601886655-1280-1098 ERROR File "/opt/beedi/beedi/../cc/qzhttputils.py", line 57, in posturl -- return post_multipart(urlparts[1], port, urlparts[2], fields,files, ownerid)
2014-10-23 06:40:20,068 studio_fone-3664-601886655-1280-1098 ERROR File "/opt/beedi/beedi/../cc/qzhttputils.py", line 16, in post_multipart -- h.request('POST', selector, body, headers)
2014-10-23 06:40:20,068 studio_fone-3664-601886655-1280-1098 ERROR File "/usr/lib/python2.7/httplib.py", line 958, in request -- self._send_request(method, url, body, headers)
2014-10-23 06:40:20,068 studio_fone-3664-601886655-1280-1098 ERROR File "/usr/lib/python2.7/httplib.py", line 992, in _send_request -- self.endheaders(body)
2014-10-23 06:40:20,068 studio_fone-3664-601886655-1280-1098 ERROR File "/usr/lib/python2.7/httplib.py", line 954, in endheaders -- self._send_output(message_body)
2014-10-23 06:40:20,068 studio_fone-3664-601886655-1280-1098 ERROR File "/usr/lib/python2.7/httplib.py", line 812, in _send_output -- msg += message_body
2014-10-23 06:40:20,068 studio_fone-3664-601886655-1280-1098 ERROR UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 512: ordinal not in range(128)
2014-10-23 06:40:20,069 studio_fone-3664-601886655-1280-1098 ERROR Traceback (most recent call last):
2014-10-23 06:40:20,069 studio_fone-3664-601886655-1280-1098 ERROR File "/opt/beedi/beedi/requestbuild.py", line 743, in run -- l_docid = UploadApp (l_binaryfile, l_ownerid, l_content_disposition)
2014-10-23 06:40:20,069 studio_fone-3664-601886655-1280-1098 ERROR File "/opt/beedi/beedi/requestbuild.py", line 311, in UploadApp -- raise IOError
2014-10-23 06:40:20,069 studio_fone-3664-601886655-1280-1098 ERROR IOError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment