当前位置: 首页>>代码示例>>Python>>正文


Python http.Request方法代码示例

本文整理汇总了Python中twisted.web.http.Request方法的典型用法代码示例。如果您正苦于以下问题:Python http.Request方法的具体用法?Python http.Request怎么用?Python http.Request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.web.http的用法示例。


在下文中一共展示了http.Request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_losingConnection

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_losingConnection(self):
        """
        Calling L{http.Request.loseConnection} causes the transport to be
        disconnected.
        """
        b = StringTransport()
        a = http.HTTPChannel()
        a.requestFactory = self.ShutdownHTTPHandler
        a.makeConnection(b)
        a.dataReceived(self.request)

        # The transport should have been shut down.
        self.assertTrue(b.disconnecting)

        # No response should have been written.
        value = b.value()
        self.assertEqual(value, b'') 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:19,代码来源:test_http.py

示例2: test_chunkedResponses

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_chunkedResponses(self):
        """
        Test that the L{HTTPChannel} correctly chunks responses when needed.
        """
        channel = http.HTTPChannel()
        req = http.Request(channel, False)
        trans = StringTransport()

        channel.transport = trans

        req.setResponseCode(200)
        req.clientproto = b"HTTP/1.1"
        req.responseHeaders.setRawHeaders(b"test", [b"lemur"])
        req.write(b'Hello')
        req.write(b'World!')

        self.assertResponseEquals(
            trans.value(),
            [(b"HTTP/1.1 200 OK",
              b"Test: lemur",
              b"Transfer-Encoding: chunked",
              b"5\r\nHello\r\n6\r\nWorld!\r\n")]) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:24,代码来源:test_http.py

示例3: test_invalidNonAsciiMethod

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_invalidNonAsciiMethod(self):
        """
        When client sends invalid HTTP method containing
        non-ascii characters HTTP 400 'Bad Request' status will be returned.
        """
        processed = []
        class MyRequest(http.Request):
            def process(self):
                processed.append(self)
                self.finish()

        badRequestLine = b"GE\xc2\xa9 / HTTP/1.1\r\n\r\n"
        channel = self.runRequest(badRequestLine, MyRequest, 0)
        self.assertEqual(
            channel.transport.value(),
            b"HTTP/1.1 400 Bad Request\r\n\r\n")
        self.assertTrue(channel.transport.disconnecting)
        self.assertEqual(processed, []) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:20,代码来源:test_http.py

示例4: test_basicAuth

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_basicAuth(self):
        """
        L{HTTPChannel} provides username and password information supplied in
        an I{Authorization} header to the L{Request} which makes it available
        via its C{getUser} and C{getPassword} methods.
        """
        requests = []
        class Request(http.Request):
            def process(self):
                self.credentials = (self.getUser(), self.getPassword())
                requests.append(self)

        for u, p in [(b"foo", b"bar"), (b"hello", b"there:z")]:
            s = base64.encodestring(b":".join((u, p))).strip()
            f = b"GET / HTTP/1.0\nAuthorization: Basic " + s + b"\n\n"
            self.runRequest(f, Request, 0)
            req = requests.pop()
            self.assertEqual((u, p), req.credentials) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:20,代码来源:test_http.py

示例5: test_headers

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_headers(self):
        """
        Headers received by L{HTTPChannel} in a request are made available to
        the L{Request}.
        """
        processed = []
        class MyRequest(http.Request):
            def process(self):
                processed.append(self)
                self.finish()

        requestLines = [
            b"GET / HTTP/1.0",
            b"Foo: bar",
            b"baz: Quux",
            b"baz: quux",
            b"",
            b""]

        self.runRequest(b'\n'.join(requestLines), MyRequest, 0)
        [request] = processed
        self.assertEqual(
            request.requestHeaders.getRawHeaders(b'foo'), [b'bar'])
        self.assertEqual(
            request.requestHeaders.getRawHeaders(b'bAz'), [b'Quux', b'quux']) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:27,代码来源:test_http.py

示例6: test_invalidContentLengthHeader

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_invalidContentLengthHeader(self):
        """
        If a Content-Length header with a non-integer value is received, a 400
        (Bad Request) response is sent to the client and the connection is
        closed.
        """
        processed = []
        class MyRequest(http.Request):
            def process(self):
                processed.append(self)
                self.finish()

        requestLines = [b"GET / HTTP/1.0", b"Content-Length: x", b"", b""]
        channel = self.runRequest(b"\n".join(requestLines), MyRequest, 0)
        self.assertEqual(
            channel.transport.value(),
            b"HTTP/1.1 400 Bad Request\r\n\r\n")
        self.assertTrue(channel.transport.disconnecting)
        self.assertEqual(processed, []) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:21,代码来源:test_http.py

示例7: test_invalidHeaderNoColon

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_invalidHeaderNoColon(self):
        """
        If a header without colon is received a 400 (Bad Request) response
        is sent to the client and the connection is closed.
        """
        processed = []
        class MyRequest(http.Request):
            def process(self):
                processed.append(self)
                self.finish()

        requestLines = [b"GET / HTTP/1.0", b"HeaderName ", b"", b""]
        channel = self.runRequest(b"\n".join(requestLines), MyRequest, 0)
        self.assertEqual(
            channel.transport.value(),
            b"HTTP/1.1 400 Bad Request\r\n\r\n")
        self.assertTrue(channel.transport.disconnecting)
        self.assertEqual(processed, []) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:20,代码来源:test_http.py

示例8: testCookies

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def testCookies(self):
        """
        Test cookies parsing and reading.
        """
        httpRequest = b'''\
GET / HTTP/1.0
Cookie: rabbit="eat carrot"; ninja=secret; spam="hey 1=1!"

'''
        cookies = {}
        testcase = self
        class MyRequest(http.Request):
            def process(self):
                for name in [b'rabbit', b'ninja', b'spam']:
                    cookies[name] = self.getCookie(name)
                testcase.didRequest = True
                self.finish()

        self.runRequest(httpRequest, MyRequest)

        self.assertEqual(
            cookies, {
                b'rabbit': b'"eat carrot"',
                b'ninja': b'secret',
                b'spam': b'"hey 1=1!"'}) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:27,代码来源:test_http.py

示例9: testGET

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def testGET(self):
        httpRequest = b'''\
GET /?key=value&multiple=two+words&multiple=more%20words&empty= HTTP/1.0

'''
        method = []
        args = []
        testcase = self
        class MyRequest(http.Request):
            def process(self):
                method.append(self.method)
                args.extend([
                        self.args[b"key"],
                        self.args[b"empty"],
                        self.args[b"multiple"]])
                testcase.didRequest = True
                self.finish()

        self.runRequest(httpRequest, MyRequest)
        self.assertEqual(method, [b"GET"])
        self.assertEqual(
            args, [[b"value"], [b""], [b"two words", b"more words"]]) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:24,代码来源:test_http.py

示例10: test_extraQuestionMark

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_extraQuestionMark(self):
        """
        While only a single '?' is allowed in an URL, several other servers
        allow several and pass all after the first through as part of the
        query arguments.  Test that we emulate this behavior.
        """
        httpRequest = b'GET /foo?bar=?&baz=quux HTTP/1.0\n\n'

        method = []
        path = []
        args = []
        testcase = self
        class MyRequest(http.Request):
            def process(self):
                method.append(self.method)
                path.append(self.path)
                args.extend([self.args[b'bar'], self.args[b'baz']])
                testcase.didRequest = True
                self.finish()

        self.runRequest(httpRequest, MyRequest)
        self.assertEqual(method, [b'GET'])
        self.assertEqual(path, [b'/foo'])
        self.assertEqual(args, [[b'?'], [b'quux']]) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:26,代码来源:test_http.py

示例11: test_missingContentDisposition

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_missingContentDisposition(self):
        """
        If the C{Content-Disposition} header is missing, the request is denied
        as a bad request.
        """
        req = b'''\
POST / HTTP/1.0
Content-Type: multipart/form-data; boundary=AaB03x
Content-Length: 103

--AaB03x
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

abasdfg
--AaB03x--
'''
        channel = self.runRequest(req, http.Request, success=False)
        self.assertEqual(
            channel.transport.value(),
            b"HTTP/1.1 400 Bad Request\r\n\r\n") 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:23,代码来源:test_http.py

示例12: _compatHeadersTest

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def _compatHeadersTest(self, oldName, newName):
        """
        Verify that each of two different attributes which are associated with
        the same state properly reflect changes made through the other.

        This is used to test that the C{headers}/C{responseHeaders} and
        C{received_headers}/C{requestHeaders} pairs interact properly.
        """
        req = http.Request(DummyChannel(), False)
        getattr(req, newName).setRawHeaders(b"test", [b"lemur"])
        self.assertEqual(getattr(req, oldName)[b"test"], b"lemur")
        setattr(req, oldName, {b"foo": b"bar"})
        self.assertEqual(
            list(getattr(req, newName).getAllRawHeaders()),
            [(b"Foo", [b"bar"])])
        setattr(req, newName, http_headers.Headers())
        self.assertEqual(getattr(req, oldName), {}) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:19,代码来源:test_http.py

示例13: test_setLastModifiedTwiceNotCached

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_setLastModifiedTwiceNotCached(self):
        """
        When L{http.Request.setLastModified} is called multiple times, the
        highest supplied value is honored. If that value is higher than the
        if-modified-since date in the request header, the method returns None.
        """
        req = http.Request(DummyChannel(), False)
        req.requestHeaders.setRawHeaders(
            networkString('if-modified-since'),
                          [b'01 Jan 1970 00:00:01 GMT']
            )
        req.setLastModified(1000000)

        result = req.setLastModified(0)

        self.assertEqual(result, None) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:18,代码来源:test_http.py

示例14: test_setLastModifiedTwiceCached

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def test_setLastModifiedTwiceCached(self):
        """
        When L{http.Request.setLastModified} is called multiple times, the
        highest supplied value is honored. If that value is lower than the
        if-modified-since date in the request header, the method returns
        L{http.CACHED}.
        """
        req = http.Request(DummyChannel(), False)
        req.requestHeaders.setRawHeaders(
            networkString('if-modified-since'),
                          [b'01 Jan 1999 00:00:01 GMT']
            )
        req.setLastModified(1)

        result = req.setLastModified(0)

        self.assertEqual(result, http.CACHED) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:19,代码来源:test_http.py

示例15: _checkCookie

# 需要导入模块: from twisted.web import http [as 别名]
# 或者: from twisted.web.http import Request [as 别名]
def _checkCookie(self, expectedCookieValue, *args, **kwargs):
        """
        Call L{http.Request.setCookie} with C{*args} and C{**kwargs}, and check
        that the cookie value is equal to C{expectedCookieValue}.
        """
        channel = DummyChannel()
        req = http.Request(channel, False)
        req.addCookie(*args, **kwargs)
        self.assertEqual(req.cookies[0], expectedCookieValue)

        # Write nothing to make it produce the headers
        req.write(b"")
        writtenLines = channel.transport.written.getvalue().split(b"\r\n")

        # There should be one Set-Cookie header
        setCookieLines = [x for x in writtenLines
                          if x.startswith(b"Set-Cookie")]
        self.assertEqual(len(setCookieLines), 1)
        self.assertEqual(setCookieLines[0],
                         b"Set-Cookie: " + expectedCookieValue) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:22,代码来源:test_http.py


注:本文中的twisted.web.http.Request方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。