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


Python DummyRequest.getAllHeaders方法代码示例

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


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

示例1: forge_request

# 需要导入模块: from twisted.web.test.requesthelper import DummyRequest [as 别名]
# 或者: from twisted.web.test.requesthelper.DummyRequest import getAllHeaders [as 别名]
def forge_request(uri=b'https://www.globaleaks.org/',
                  headers=None, body='', client_addr=None, method=b'GET'):
    """
    Creates a twisted.web.Request compliant request that is from an external
    IP address.
    """
    if headers is None:
        headers = {}

    _, host, path, query, frag = urlsplit(uri)

    x = host.split(b':')
    if len(x) > 1:
        port = int(x[1])
    else:
        port = 80

    request = DummyRequest([b''])
    request.tid = 1
    request.method = method
    request.uri = uri
    request.path = path
    request._serverName = host

    request.code = 200
    request.client_ip = b'127.0.0.1'
    request.client_proto = b'https'
    request.client_using_tor = False

    def getResponseBody():
        # Ugh, hack. Twisted returns this all as bytes, and we want it as str
        if isinstance(request.written[0], binary_type):
            return b''.join(request.written)
        else:
            return ''.join(request.written)

    request.getResponseBody = getResponseBody

    if client_addr is None:
        request.client = IPv4Address('TCP', b'1.2.3.4', 12345)
    else:
        request.client = client_addr

    def getHost():
        return IPv4Address('TCP', b'127.0.0.1', port)

    request.getHost = getHost

    def notifyFinish():
        return Deferred()

    request.notifyFinish = notifyFinish

    request.requestHeaders.setRawHeaders('host', [b'127.0.0.1'])
    request.requestHeaders.setRawHeaders('user-agent', [b'NSA Agent'])

    for k, v in headers.items():
        request.requestHeaders.setRawHeaders(k, [v])

    request.headers = request.getAllHeaders()

    class fakeBody(object):
        def read(self):
            if isinstance(body, dict):
                ret = json.dumps(body)
            else:
                ret = body

            if isinstance(ret, text_type):
                ret = ret.encode('utf-8')

            return ret

        def close(self):
            pass

    request.content = fakeBody()

    return request
开发者ID:globaleaks,项目名称:GlobaLeaks,代码行数:81,代码来源:helpers.py


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