當前位置: 首頁>>代碼示例>>Python>>正文


Python HTTPHeaders.parse方法代碼示例

本文整理匯總了Python中tornado.httputil.HTTPHeaders.parse方法的典型用法代碼示例。如果您正苦於以下問題:Python HTTPHeaders.parse方法的具體用法?Python HTTPHeaders.parse怎麽用?Python HTTPHeaders.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tornado.httputil.HTTPHeaders的用法示例。


在下文中一共展示了HTTPHeaders.parse方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_future_interface

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_future_interface(self):
        """Basic test of IOStream's ability to return Futures."""
        stream = self._make_client_iostream()
        connect_result = yield stream.connect(
            ("127.0.0.1", self.get_http_port()))
        self.assertIs(connect_result, stream)
        yield stream.write(b"GET / HTTP/1.0\r\n\r\n")
        first_line = yield stream.read_until(b"\r\n")
        self.assertEqual(first_line, b"HTTP/1.1 200 OK\r\n")
        # callback=None is equivalent to no callback.
        header_data = yield stream.read_until(b"\r\n\r\n", callback=None)
        headers = HTTPHeaders.parse(header_data.decode('latin1'))
        content_length = int(headers['Content-Length'])
        body = yield stream.read_bytes(content_length)
        self.assertEqual(body, b'Hello')
        stream.close() 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:18,代碼來源:iostream_test.py

示例2: test_multi_line

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_multi_line(self):
        # Lines beginning with whitespace are appended to the previous line
        # with any leading whitespace replaced by a single space.
        # Note that while multi-line headers are a part of the HTTP spec,
        # their use is strongly discouraged.
        data = """\
Foo: bar
 baz
Asdf: qwer
\tzxcv
Foo: even
     more
     lines
""".replace("\n", "\r\n")
        headers = HTTPHeaders.parse(data)
        self.assertEqual(headers["asdf"], "qwer zxcv")
        self.assertEqual(headers.get_list("asdf"), ["qwer zxcv"])
        self.assertEqual(headers["Foo"], "bar baz,even more lines")
        self.assertEqual(headers.get_list("foo"), ["bar baz", "even more lines"])
        self.assertEqual(sorted(list(headers.get_all())),
                         [("Asdf", "qwer zxcv"),
                          ("Foo", "bar baz"),
                          ("Foo", "even more lines")]) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:25,代碼來源:httputil_test.py

示例3: test_unix_socket

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_unix_socket(self):
        sockfile = os.path.join(self.tmpdir, "test.sock")
        sock = netutil.bind_unix_socket(sockfile)
        app = Application([("/hello", HelloWorldRequestHandler)])
        server = HTTPServer(app, io_loop=self.io_loop)
        server.add_socket(sock)
        stream = IOStream(socket.socket(socket.AF_UNIX), io_loop=self.io_loop)
        stream.connect(sockfile, self.stop)
        self.wait()
        stream.write(b"GET /hello HTTP/1.0\r\n\r\n")
        stream.read_until(b"\r\n", self.stop)
        response = self.wait()
        self.assertEqual(response, b"HTTP/1.0 200 OK\r\n")
        stream.read_until(b"\r\n\r\n", self.stop)
        headers = HTTPHeaders.parse(self.wait().decode('latin1'))
        stream.read_bytes(int(headers["Content-Length"]), self.stop)
        body = self.wait()
        self.assertEqual(body, b"Hello world")
        stream.close()
        server.stop() 
開發者ID:viewfinderco,項目名稱:viewfinder,代碼行數:22,代碼來源:httpserver_test.py

示例4: test_unix_socket

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_unix_socket(self):
        sockfile = os.path.join(self.tmpdir, "test.sock")
        sock = netutil.bind_unix_socket(sockfile)
        app = Application([("/hello", HelloWorldRequestHandler)])
        server = HTTPServer(app, io_loop=self.io_loop)
        server.add_socket(sock)
        stream = IOStream(socket.socket(socket.AF_UNIX), io_loop=self.io_loop)
        stream.connect(sockfile, self.stop)
        self.wait()
        stream.write(b("GET /hello HTTP/1.0\r\n\r\n"))
        stream.read_until(b("\r\n"), self.stop)
        response = self.wait()
        self.assertEqual(response, b("HTTP/1.0 200 OK\r\n"))
        stream.read_until(b("\r\n\r\n"), self.stop)
        headers = HTTPHeaders.parse(self.wait().decode('latin1'))
        stream.read_bytes(int(headers["Content-Length"]), self.stop)
        body = self.wait()
        self.assertEqual(body, b("Hello world"))
        stream.close()
        server.stop() 
開發者ID:omererdem,項目名稱:honeything,代碼行數:22,代碼來源:httpserver_test.py

示例5: test_unicode_newlines

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_unicode_newlines(self):
        # Ensure that only \r\n is recognized as a header separator, and not
        # the other newline-like unicode characters.
        # Characters that are likely to be problematic can be found in
        # http://unicode.org/standard/reports/tr13/tr13-5.html
        # and cpython's unicodeobject.c (which defines the implementation
        # of unicode_type.splitlines(), and uses a different list than TR13).
        newlines = [
            u('\u001b'),  # VERTICAL TAB
            u('\u001c'),  # FILE SEPARATOR
            u('\u001d'),  # GROUP SEPARATOR
            u('\u001e'),  # RECORD SEPARATOR
            u('\u0085'),  # NEXT LINE
            u('\u2028'),  # LINE SEPARATOR
            u('\u2029'),  # PARAGRAPH SEPARATOR
        ]
        for newline in newlines:
            # Try the utf8 and latin1 representations of each newline
            for encoding in ['utf8', 'latin1']:
                try:
                    try:
                        encoded = newline.encode(encoding)
                    except UnicodeEncodeError:
                        # Some chars cannot be represented in latin1
                        continue
                    data = b'Cookie: foo=' + encoded + b'bar'
                    # parse() wants a native_str, so decode through latin1
                    # in the same way the real parser does.
                    headers = HTTPHeaders.parse(
                        native_str(data.decode('latin1')))
                    expected = [('Cookie', 'foo=' +
                                 native_str(encoded.decode('latin1')) + 'bar')]
                    self.assertEqual(
                        expected, list(headers.get_all()))
                except Exception:
                    gen_log.warning("failed while trying %r in %s",
                                    newline, encoding)
                    raise 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:40,代碼來源:httputil_test.py

示例6: test_optional_cr

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_optional_cr(self):
        # Both CRLF and LF should be accepted as separators. CR should not be
        # part of the data when followed by LF, but it is a normal char
        # otherwise (or should bare CR be an error?)
        headers = HTTPHeaders.parse(
            'CRLF: crlf\r\nLF: lf\nCR: cr\rMore: more\r\n')
        self.assertEqual(sorted(headers.get_all()),
                         [('Cr', 'cr\rMore: more'),
                          ('Crlf', 'crlf'),
                          ('Lf', 'lf'),
                          ]) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:13,代碼來源:httputil_test.py

示例7: test_100_continue

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_100_continue(self):
        # Run through a 100-continue interaction by hand:
        # When given Expect: 100-continue, we get a 100 response after the
        # headers, and then the real response after the body.
        stream = IOStream(socket.socket(), io_loop=self.io_loop)
        stream.connect(("127.0.0.1", self.get_http_port()), callback=self.stop)
        self.wait()
        stream.write(b"\r\n".join([b"POST /hello HTTP/1.1",
                                   b"Content-Length: 1024",
                                   b"Expect: 100-continue",
                                   b"Connection: close",
                                   b"\r\n"]), callback=self.stop)
        self.wait()
        stream.read_until(b"\r\n\r\n", self.stop)
        data = self.wait()
        self.assertTrue(data.startswith(b"HTTP/1.1 100 "), data)
        stream.write(b"a" * 1024)
        stream.read_until(b"\r\n", self.stop)
        first_line = self.wait()
        self.assertTrue(first_line.startswith(b"HTTP/1.1 200"), first_line)
        stream.read_until(b"\r\n\r\n", self.stop)
        header_data = self.wait()
        headers = HTTPHeaders.parse(native_str(header_data.decode('latin1')))
        stream.read_bytes(int(headers["Content-Length"]), self.stop)
        body = self.wait()
        self.assertEqual(body, b"Got 1024 bytes in POST")
        stream.close() 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:29,代碼來源:httpserver_test.py

示例8: test_unix_socket

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_unix_socket(self):
        self.stream.write(b"GET /hello HTTP/1.0\r\n\r\n")
        self.stream.read_until(b"\r\n", self.stop)
        response = self.wait()
        self.assertEqual(response, b"HTTP/1.1 200 OK\r\n")
        self.stream.read_until(b"\r\n\r\n", self.stop)
        headers = HTTPHeaders.parse(self.wait().decode('latin1'))
        self.stream.read_bytes(int(headers["Content-Length"]), self.stop)
        body = self.wait()
        self.assertEqual(body, b"Hello world") 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:12,代碼來源:httpserver_test.py

示例9: read_headers

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def read_headers(self):
        self.stream.read_until(b'\r\n', self.stop)
        first_line = self.wait()
        self.assertTrue(first_line.startswith(b'HTTP/1.1 200'), first_line)
        self.stream.read_until(b'\r\n\r\n', self.stop)
        header_bytes = self.wait()
        headers = HTTPHeaders.parse(header_bytes.decode('latin1'))
        return headers 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:10,代碼來源:httpserver_test.py

示例10: test_gzip_unsupported

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_gzip_unsupported(self):
        # Gzip support is opt-in; without it the server fails to parse
        # the body (but parsing form bodies is currently just a log message,
        # not a fatal error).
        with ExpectLog(gen_log, "Unsupported Content-Encoding"):
            response = self.post_gzip('foo=bar')
        self.assertEquals(json_decode(response.body), {}) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:9,代碼來源:httpserver_test.py

示例11: test_double_slash

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_double_slash(self):
        # urlparse.urlsplit (which tornado.httpserver used to use
        # incorrectly) would parse paths beginning with "//" as
        # protocol-relative urls.
        response = self.fetch("//doubleslash")
        self.assertEqual(200, response.code)
        self.assertEqual(json_decode(response.body), {}) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:9,代碼來源:httpserver_test.py

示例12: test_future_interface

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_future_interface(self):
        """Basic test of IOStream's ability to return Futures."""
        stream = self._make_client_iostream()
        connect_result = yield stream.connect(("127.0.0.1", self.get_http_port()))
        self.assertIs(connect_result, stream)
        yield stream.write(b"GET / HTTP/1.0\r\n\r\n")
        first_line = yield stream.read_until(b"\r\n")
        self.assertEqual(first_line, b"HTTP/1.1 200 OK\r\n")
        # callback=None is equivalent to no callback.
        header_data = yield stream.read_until(b"\r\n\r\n")
        headers = HTTPHeaders.parse(header_data.decode("latin1"))
        content_length = int(headers["Content-Length"])
        body = yield stream.read_bytes(content_length)
        self.assertEqual(body, b"Hello")
        stream.close() 
開發者ID:opendevops-cn,項目名稱:opendevops,代碼行數:17,代碼來源:iostream_test.py

示例13: test_100_continue

# 需要導入模塊: from tornado.httputil import HTTPHeaders [as 別名]
# 或者: from tornado.httputil.HTTPHeaders import parse [as 別名]
def test_100_continue(self):
        # Run through a 100-continue interaction by hand:
        # When given Expect: 100-continue, we get a 100 response after the
        # headers, and then the real response after the body.
        stream = IOStream(socket.socket(), io_loop=self.io_loop)
        stream.connect(("localhost", self.get_http_port()), callback=self.stop)
        self.wait()
        stream.write(b"\r\n".join([b"POST /hello HTTP/1.1",
                                   b"Content-Length: 1024",
                                   b"Expect: 100-continue",
                                   b"Connection: close",
                                   b"\r\n"]), callback=self.stop)
        self.wait()
        stream.read_until(b"\r\n\r\n", self.stop)
        data = self.wait()
        self.assertTrue(data.startswith(b"HTTP/1.1 100 "), data)
        stream.write(b"a" * 1024)
        stream.read_until(b"\r\n", self.stop)
        first_line = self.wait()
        self.assertTrue(first_line.startswith(b"HTTP/1.1 200"), first_line)
        stream.read_until(b"\r\n\r\n", self.stop)
        header_data = self.wait()
        headers = HTTPHeaders.parse(native_str(header_data.decode('latin1')))
        stream.read_bytes(int(headers["Content-Length"]), self.stop)
        body = self.wait()
        self.assertEqual(body, b"Got 1024 bytes in POST")
        stream.close() 
開發者ID:viewfinderco,項目名稱:viewfinder,代碼行數:29,代碼來源:httpserver_test.py


注:本文中的tornado.httputil.HTTPHeaders.parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。