def post_mime(url, httplib=default_httplib, timeout=DEFAULT_TIMEOUT, retries=5, **kwargs): body = [] crlf = '\r\n' boundary = "graphBoundary" # UTF8 params utf8_kwargs = dict([(k, v.encode('UTF-8')) for (k,v) in kwargs.iteritems() if k != 'file' and v is not None]) # Add args for (k,v) in utf8_kwargs.iteritems(): body.append("--"+boundary) body.append('Content-Disposition: form-data; name="%s"' % k) body.append('') body.append(str(v)) # Add raw data file = kwargs.get('file') if file: file.open() data = file.read() file.close() body.append("--"+boundary) body.append('Content-Disposition: form-data; filename="facegraphfile.png"') body.append('') body.append(data) body.append("--"+boundary+"--") body.append('') body = crlf.join(body) # Post to server kwargs = {} if timeout: kwargs = {'timeout': timeout} r = httplib.HTTPSConnection(get_host(url), **kwargs) headers = {'Content-Type': 'multipart/form-data; boundary=%s' % boundary, 'Content-Length': str(len(body)), 'MIME-Version': '1.0'} r.request('POST', get_path(url).encode(), body, headers) attempt = 0 while True: try: response = r.getresponse().read() return json.loads(response) except JSONDecodeError, e: if len(e.doc) == 0: raise EmptyStringReturnedException(str(e)) else: raise WrappedJSONDecodeError(response, e) except (httplib.BadStatusLine, IOError): if attempt < retries: attempt += 1 else: raise
def test_get_host(self): self.assertEquals('a.com', ops.get_host('http://a.com')) self.assertEquals('a.com', ops.get_host('http://a.com/a/b')) self.assertEquals('a.com', ops.get_host('http://a.com/a?a=b'))
def post_mime(url, httplib=default_httplib, timeout=DEFAULT_TIMEOUT, retries=5, **kwargs): body = [] crlf = '\r\n' boundary = "graphBoundary" # UTF8 params utf8_kwargs = dict([(k, v.encode('UTF-8')) for (k, v) in kwargs.iteritems() if k != 'file' and v is not None]) # Add args for (k, v) in utf8_kwargs.iteritems(): body.append("--" + boundary) body.append('Content-Disposition: form-data; name="%s"' % k) body.append('') body.append(str(v)) # Add raw data file = kwargs.get('file') if file: file.open() data = file.read() file.close() body.append("--" + boundary) body.append( 'Content-Disposition: form-data; filename="facegraphfile.png"') body.append('') body.append(data) body.append("--" + boundary + "--") body.append('') body = crlf.join(body) # Post to server kwargs = {} if timeout: kwargs = {'timeout': timeout} r = httplib.HTTPSConnection(get_host(url), **kwargs) headers = { 'Content-Type': 'multipart/form-data; boundary=%s' % boundary, 'Content-Length': str(len(body)), 'MIME-Version': '1.0' } r.request('POST', get_path(url).encode(), body, headers) attempt = 0 while True: try: return r.getresponse().read() except (httplib.BadStatusLine, IOError): if attempt < retries: attempt += 1 else: raise finally: r.close()