def send(self): """ Post fields and files to an HTTP server as multipart/form-data. Return the server's response. """ scheme, location, path, query, _ = urlparse.urlsplit(self.url) assert scheme in ("http", "https"), "Unsupported scheme %r" % scheme content_type, body = self._encode_multipart_formdata() handle = getattr(httplib, scheme.upper() + "Connection")(location) if self.mock_http: # Don't actually send anything, print to stdout instead handle.sock = parts.Bunch( sendall=lambda x: sys.stdout.write( fmt.to_utf8( "".join( (c if 32 <= ord(c) < 127 or ord(c) in (8, 10) else u"\u27ea%02X\u27eb" % ord(c)) for c in x ) ) ), makefile=lambda dummy, _: StringIO.StringIO( "\r\n".join(("HTTP/1.0 204 NO CONTENT", "Content-Length: 0", "")) ), close=lambda: None, ) handle.putrequest("POST", urlparse.urlunsplit(("", "", path, query, ""))) handle.putheader("Content-Type", content_type) handle.putheader("Content-Length", str(len(body))) for key, val in self.headers.items(): handle.putheader(key, val) handle.endheaders() handle.send(body) # print handle.__dict__ return handle.getresponse()
def test_to_utf8(self): cases = [ ("", ""), (u"", u""), (False, False), (None, None), (u"\xea", b"\xc3\xaa",), (u"\u20ac", b"\xe2\x82\xac"), (b"\xc3\xaa", b"\xc3\xaa"), (b"\xfe\xff\x00\x20", u" "), (b"\xff\xfe\x20\x00", u" "), (b"\xef\xbb\xbf\x20", u" "), #(b"\xc3\xc3\x81".decode('cp1252'), "\xc3\xc3\x81"), ] for val, expected in cases: result = fmt.to_utf8(val) assert result == expected
def test_to_utf8(self): cases = [ ("", ""), (u"", u""), (False, False), (None, None), ( u"\xea", "\xc3\xaa", ), (u"\u20ac", "\xe2\x82\xac"), ("\xc3\xaa", "\xc3\xaa"), ("\xfe\xff\x00\x20", u" "), ("\xff\xfe\x20\x00", u" "), ("\xef\xbb\xbf\x20", u" "), ("\xc3\xc3\x81", "\xc3\xc3\x81"), ] for val, expected in cases: result = fmt.to_utf8(val) assert result == expected
def _encode_multipart_formdata(self): """ Encode POST body. Return (content_type, body) ready for httplib.HTTP instance """ def get_content_type(filename): "Helper to get MIME type." return mimetypes.guess_type( filename)[0] or 'application/octet-stream' boundary = '----------ThIs_Is_tHe_b0uNdaRY_%d$' % (time.time()) logical_lines = [] for name, value in self.fields: if value is None: continue logical_lines.append('--' + boundary) if hasattr(value, "read"): filename = getattr(value, "name", str(id(value)) + ".dat") logical_lines.append( 'Content-Disposition: form-data; name="%s"; filename="%s"' % (name, os.path.basename(filename).replace( "'", '_').replace('"', '_'))) logical_lines.append('Content-Type: %s' % get_content_type(filename)) logical_lines.append('Content-Transfer-Encoding: binary') value = value.read() else: logical_lines.append( 'Content-Disposition: form-data; name="%s"' % name) logical_lines.append( 'Content-Type: text/plain; charset="UTF-8"') value = fmt.to_utf8(value) #logical_lines.append('Content-Length: %d' % len(value)) logical_lines.append('') logical_lines.append(value) logical_lines.append('--' + boundary + '--') logical_lines.append('') body = '\r\n'.join(logical_lines) content_type = 'multipart/form-data; boundary=%s' % boundary return content_type, body
def _encode_multipart_formdata(self): """ Encode POST body. Return (content_type, body) ready for httplib.HTTP instance """ def get_content_type(filename): "Helper to get MIME type." return mimetypes.guess_type(filename)[0] or "application/octet-stream" boundary = "----------ThIs_Is_tHe_b0uNdaRY_%d$" % (time.time()) logical_lines = [] for name, value in self.fields: if value is None: continue logical_lines.append("--" + boundary) if hasattr(value, "read"): filename = getattr(value, "name", str(id(value)) + ".dat") logical_lines.append( 'Content-Disposition: form-data; name="%s"; filename="%s"' % (name, os.path.basename(filename).replace("'", "_").replace('"', "_")) ) logical_lines.append("Content-Type: %s" % get_content_type(filename)) logical_lines.append("Content-Transfer-Encoding: binary") value = value.read() else: logical_lines.append('Content-Disposition: form-data; name="%s"' % name) logical_lines.append('Content-Type: text/plain; charset="UTF-8"') value = fmt.to_utf8(value) # logical_lines.append('Content-Length: %d' % len(value)) logical_lines.append("") logical_lines.append(value) logical_lines.append("--" + boundary + "--") logical_lines.append("") body = "\r\n".join(logical_lines) content_type = "multipart/form-data; boundary=%s" % boundary return content_type, body
def send(self): """ Post fields and files to an HTTP server as multipart/form-data. Return the server's response. """ scheme, location, path, query, _ = urllib.parse.urlsplit(self.url) assert scheme in ("http", "https"), "Unsupported scheme %r" % scheme content_type, body = self._encode_multipart_formdata() handle = getattr(httplib, scheme.upper() + "Connection")(location) if self.mock_http: # Don't actually send anything, print to stdout instead handle.sock = parts.Bunch( sendall=lambda x: sys.stdout.write( fmt.to_utf8(''.join( (c if 32 <= ord(c) < 127 or ord(c) in (8, 10) else u'\u27ea%02X\u27eb' % ord(c)) for c in x))), makefile=lambda dummy, _: StringIO.StringIO("\r\n".join(( "HTTP/1.0 204 NO CONTENT", "Content-Length: 0", "", ))), close=lambda: None, ) handle.putrequest('POST', urllib.parse.urlunsplit(('', '', path, query, ''))) handle.putheader('Content-Type', content_type) handle.putheader('Content-Length', str(len(body))) for key, val in self.headers.items(): handle.putheader(key, val) handle.endheaders() handle.send(body) #print handle.__dict__ return handle.getresponse()
def test_fmt_to_utf8(val, expected): result = fmt.to_utf8(val) assert result == expected
definition = None objtype = type(value) if objtype is list: value = [rc_quoted(x) for x in value] wrap_fmt = '((%s))' if value and is_method( value[0]) else '{%s}' definition = wrap_fmt % ', '.join(value) elif objtype is dict: print('method.insert = {}, multi|rlookup|static'. format(name)) for key, val in sorted(value.items()): val = rc_quoted(val) if len(val) > self.RC_CONTINUATION_THRESHOLD: val = '\\\n ' + val print(b'method.set_key = {}, "{}", {}'.format( name, fmt.to_utf8(key), fmt.to_utf8(val))) elif objtype is str: definition = rc_quoted(value) elif objtype is int: definition = '{:d}'.format(value) else: self.LOG.error( "Cannot handle {!r} definition of method {}". format(objtype, name)) continue if definition: if name in builtins: print('{}.set = {}'.format(name, definition)) else: rctype = {