Python multidict.CIMultiDict() Examples

The following are 30 code examples of multidict.CIMultiDict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module multidict , or try the search function .
Example #1
Source File: test_tanner_handler_parse_tanner_response.py    From snare with GNU General Public License v3.0 6 votes vote down vote up
def test_parse_type_two_with_headers(self):
        self.detection = {
            "type": 2,
            "payload": {
                "page": "",
                "value": "test.png",
                "headers": {
                    "content-type": "multipart/form-data",
                },
            },
        }
        self.expected_content = b'test.png'
        self.content_type = 'image/png'
        self.headers = multidict.CIMultiDict([("Content-Type", "multipart/form-data")])

        async def test():
            (self.res1, self.res2, self.res3) = \
                await self.handler.parse_tanner_response(
                    self.requested_name, self.detection)

        self.loop.run_until_complete(test())
        real_result = [self.res1, self.res2, self.res3]
        expected_result = [self.expected_content, self.headers, self.status_code]

        self.assertCountEqual(real_result, expected_result) 
Example #2
Source File: support.py    From molotov with Apache License 2.0 6 votes vote down vote up
def Response(method="GET", status=200, body=b"***"):
    response = LoggedClientResponse(method, URL("/"), **_respkw())
    response.status = status
    response.reason = ""
    response.code = status
    response.should_close = False
    response._headers = CIMultiDict({})
    response._raw_headers = []

    class Body:
        async def read(self):
            return body

        def unread_data(self, data):
            if body == b"":
                err = AttributeError(
                    "'EmptyStreamReader' object has no " "attribute 'unread_data'"
                )
                raise err
            pass

    response.content = Body()
    response._content = body

    return response 
Example #3
Source File: message.py    From aiosip with Apache License 2.0 6 votes vote down vote up
def from_request(cls, request, status_code, status_message, payload=None, headers=None):

        if not headers:
            headers = CIMultiDict()

        if 'Via' not in headers:
            headers['Via'] = request.headers['Via']

        return Response(
            status_code=status_code,
            status_message=status_message,
            cseq=request.cseq,
            method=request.method,
            headers=headers,
            from_details=request.from_details,
            to_details=request.to_details,
            contact_details=request.contact_details,
            payload=payload,
        ) 
Example #4
Source File: testing.py    From async-asgi-testclient with MIT License 6 votes vote down vote up
def __init__(
        self,
        application,
        use_cookies: bool = True,
        timeout: Optional[Union[int, float]] = None,
        headers: Optional[Union[dict, CIMultiDict]] = None,
        scope: Optional[dict] = None,
    ):
        self.application = guarantee_single_callable(application)
        self.cookie_jar: Optional[
            SimpleCookie
        ] = SimpleCookie() if use_cookies else None
        self.timeout = timeout
        self.headers = headers or {}
        self._scope = scope or {}
        self._lifespan_input_queue: asyncio.Queue[dict] = asyncio.Queue()
        self._lifespan_output_queue: asyncio.Queue[dict] = asyncio.Queue() 
Example #5
Source File: browser.py    From crocoite with MIT License 6 votes vote down vote up
def fromRequestWillBeSent (self, req):
        """ Set request data from Chrome Network.requestWillBeSent event """
        r = req['request']

        self.id = req['requestId']
        self.url = URL (r['url'])
        self.resourceType = req.get ('type')
        self._time = ReferenceTimestamp (req['timestamp'], req['wallTime'])

        assert self.request is None, req
        self.request = Request ()
        self.request.initiator = req['initiator']
        self.request.headers = CIMultiDict (self._unfoldHeaders (r['headers']))
        self.request.hasPostData = r.get ('hasPostData', False)
        self.request.method = r['method']
        self.request.timestamp = self._time (req['timestamp'])
        if self.request.hasPostData:
            postData = r.get ('postData')
            if postData is not None:
                self.request.body = UnicodeBody (postData) 
Example #6
Source File: test_tanner_handler_parse_tanner_response.py    From snare with GNU General Public License v3.0 6 votes vote down vote up
def test_parse_type_one_error(self):
        self.requested_name = 'something/'
        self.detection = {"type": 1}
        self.expected_content = None
        self.headers = multidict.CIMultiDict()
        self.status_code = 404

        async def test():
            (self.res1, self.res2, self.res3) = \
                await self.handler.parse_tanner_response(
                    self.requested_name, self.detection)

        self.loop.run_until_complete(test())
        real_result = [self.res1, self.res2, self.res3]
        expected_result = [self.expected_content, self.headers, self.status_code]
        self.assertCountEqual(real_result, expected_result) 
Example #7
Source File: test_tanner_handler_parse_tanner_response.py    From snare with GNU General Public License v3.0 6 votes vote down vote up
def test_parse_type_three(self):
        self.detection = {
            "type": 3,
            "payload": {
                "page": "/index.html",
                "value": "test",
                "status_code": 200,
            },
        }
        self.expected_content = None
        self.headers = multidict.CIMultiDict()

        async def test():
            (self.res1, self.res2, self.res3) = \
                await self.handler.parse_tanner_response(
                    self.requested_name, self.detection)

        self.loop.run_until_complete(test())
        real_result = [self.res1, self.res2, self.res3]
        expected_result = [self.expected_content, self.headers, self.status_code]
        self.assertCountEqual(real_result, expected_result) 
Example #8
Source File: wuy.py    From wuy with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, status, content, headers=None):
        self.status = status

        if headers is None:
            self.headers = CIMultiDict()
            if content is not None and type(content) == bytes:
                self.headers["Content-Type"] = "application/octet-stream"
            else:
                self.headers["Content-Type"] = "text/html"
        else:
            if type(headers) == str:
                self.headers = CIMultiDict([("Content-Type", headers)])
            else:
                self.headers = headers

        self.content = content 
Example #9
Source File: web_response.py    From Galaxy_Plugin_Bethesda with MIT License 6 votes vote down vote up
def __init__(self, *,
                 status: int=200,
                 reason: Optional[str]=None,
                 headers: Optional[LooseHeaders]=None) -> None:
        self._body = None
        self._keep_alive = None  # type: Optional[bool]
        self._chunked = False
        self._compression = False
        self._compression_force = None  # type: Optional[ContentCoding]
        self._cookies = SimpleCookie()

        self._req = None  # type: Optional[BaseRequest]
        self._payload_writer = None  # type: Optional[AbstractStreamWriter]
        self._eof_sent = False
        self._body_length = 0
        self._state = {}  # type: Dict[str, Any]

        if headers is not None:
            self._headers = CIMultiDict(headers)  # type: CIMultiDict[str]
        else:
            self._headers = CIMultiDict()  # type: CIMultiDict[str]

        self.set_status(status, reason) 
Example #10
Source File: client.py    From Galaxy_Plugin_Bethesda with MIT License 6 votes vote down vote up
def _prepare_headers(
            self,
            headers: Optional[LooseHeaders]) -> 'CIMultiDict[str]':
        """ Add default headers and transform it to CIMultiDict
        """
        # Convert headers to MultiDict
        result = CIMultiDict(self._default_headers)
        if headers:
            if not isinstance(headers, (MultiDictProxy, MultiDict)):
                headers = CIMultiDict(headers)
            added_names = set()  # type: Set[str]
            for key, value in headers.items():
                if key in added_names:
                    result.add(key, value)
                else:
                    result[key] = value
                    added_names.add(key)
        return result 
Example #11
Source File: client.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def _prepare_headers(self, headers):
        """ Add default headers and transform it to CIMultiDict
        """
        # Convert headers to MultiDict
        result = CIMultiDict(self._default_headers)
        if headers:
            if not isinstance(headers, (MultiDictProxy, MultiDict)):
                headers = CIMultiDict(headers)
            added_names = set()
            for key, value in headers.items():
                if key in added_names:
                    result.add(key, value)
                else:
                    result[key] = value
                    added_names.add(key)
        return result 
Example #12
Source File: server.py    From annotated-py-projects with MIT License 6 votes vote down vote up
def on_headers_complete(self):
        remote_addr = self.transport.get_extra_info('peername')
        if remote_addr:
            self.headers.append(('Remote-Addr', '%s:%s' % remote_addr))

        #
        # 构建 HTTP 请求
        #
        self.request = Request(
            url_bytes=self.url,
            headers=CIMultiDict(self.headers),
            version=self.parser.get_http_version(),
            method=self.parser.get_method().decode()
        )

    #
    # HTTP 请求: 写入 body 部分
    # 
Example #13
Source File: parse.py    From cocrawler with Apache License 2.0 6 votes vote down vote up
def do_parser(body, body_bytes, resp_headers, url, crawler):
    if len(body) > int(config.read('Multiprocess', 'ParseInBurnerSize')):
        stats.stats_sum('parser in burner thread', 1)
        # headers is a multidict.CIMultiDictProxy case-blind dict
        # and the Proxy form of it doesn't pickle, so convert to one that does
        resp_headers = multidict.CIMultiDict(resp_headers)
        links, embeds, sha1, facets, base = await crawler.burner.burn(
            partial(do_burner_work_html, body, body_bytes, resp_headers,
                    burn_prefix='burner ', url=url),
            url=url)
    else:
        stats.stats_sum('parser in main thread', 1)
        # no coroutine state because this is a burn, not an await
        links, embeds, sha1, facets, base = do_burner_work_html(
            body, body_bytes, resp_headers, burn_prefix='main ', url=url)

    return links, embeds, sha1, facets, base 
Example #14
Source File: web_response.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def __init__(self, *, status=200, reason=None, headers=None):
        self._body = None
        self._keep_alive = None
        self._chunked = False
        self._compression = False
        self._compression_force = False
        self._cookies = SimpleCookie()

        self._req = None
        self._payload_writer = None
        self._eof_sent = False
        self._body_length = 0

        if headers is not None:
            self._headers = CIMultiDict(headers)
        else:
            self._headers = CIMultiDict()

        self.set_status(status, reason) 
Example #15
Source File: client.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def _prepare_headers(self, headers):
        """ Add default headers and transform it to CIMultiDict
        """
        # Convert headers to MultiDict
        result = CIMultiDict(self._default_headers)
        if headers:
            if not isinstance(headers, (MultiDictProxy, MultiDict)):
                headers = CIMultiDict(headers)
            added_names = set()
            for key, value in headers.items():
                if key in added_names:
                    result.add(key, value)
                else:
                    result[key] = value
                    added_names.add(key)
        return result 
Example #16
Source File: web_response.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def __init__(self, *, status=200, reason=None, headers=None):
        self._body = None
        self._keep_alive = None
        self._chunked = False
        self._compression = False
        self._compression_force = False
        self._cookies = SimpleCookie()

        self._req = None
        self._payload_writer = None
        self._eof_sent = False
        self._body_length = 0

        if headers is not None:
            self._headers = CIMultiDict(headers)
        else:
            self._headers = CIMultiDict()

        self.set_status(status, reason) 
Example #17
Source File: payload.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def set_content_disposition(self, disptype, quote_fields=True, **params):
        """Sets ``Content-Disposition`` header.

        :param str disptype: Disposition type: inline, attachment, form-data.
                            Should be valid extension token (see RFC 2183)
        :param dict params: Disposition params
        """
        if self._headers is None:
            self._headers = CIMultiDict()

        self._headers[hdrs.CONTENT_DISPOSITION] = content_disposition_header(
            disptype, quote_fields=quote_fields, **params) 
Example #18
Source File: payload.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def __init__(self, value, *, headers=None, content_type=sentinel,
                 filename=None, encoding=None, **kwargs):
        self._value = value
        self._encoding = encoding
        self._filename = filename
        if headers is not None:
            self._headers = CIMultiDict(headers)
            if content_type is sentinel and hdrs.CONTENT_TYPE in self._headers:
                content_type = self._headers[hdrs.CONTENT_TYPE]

        if content_type is sentinel:
            content_type = None

        self._content_type = content_type 
Example #19
Source File: web_request.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def clone(self, *, method=sentinel, rel_url=sentinel,
              headers=sentinel):
        """Clone itself with replacement some attributes.

        Creates and returns a new instance of Request object. If no parameters
        are given, an exact copy is returned. If a parameter is not passed, it
        will reuse the one from the current request object.

        """

        if self._read_bytes:
            raise RuntimeError("Cannot clone request "
                               "after reading it's content")

        dct = {}
        if method is not sentinel:
            dct['method'] = method
        if rel_url is not sentinel:
            rel_url = URL(rel_url)
            dct['url'] = rel_url
            dct['path'] = str(rel_url)
        if headers is not sentinel:
            dct['headers'] = CIMultiDict(headers)
            dct['raw_headers'] = tuple((k.encode('utf-8'), v.encode('utf-8'))
                                       for k, v in headers.items())

        message = self._message._replace(**dct)

        return self.__class__(
            message,
            self._payload,
            self._protocol,
            self._writer,
            self._time_service,
            self._task,
            secure_proxy_ssl_header=self._secure_proxy_ssl_header) 
Example #20
Source File: web_request.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def clone(self, *, method=sentinel, rel_url=sentinel,
              headers=sentinel):
        """Clone itself with replacement some attributes.

        Creates and returns a new instance of Request object. If no parameters
        are given, an exact copy is returned. If a parameter is not passed, it
        will reuse the one from the current request object.

        """

        if self._read_bytes:
            raise RuntimeError("Cannot clone request "
                               "after reading it's content")

        dct = {}
        if method is not sentinel:
            dct['method'] = method
        if rel_url is not sentinel:
            rel_url = URL(rel_url)
            dct['url'] = rel_url
            dct['path'] = str(rel_url)
        if headers is not sentinel:
            dct['headers'] = CIMultiDict(headers)
            dct['raw_headers'] = tuple((k.encode('utf-8'), v.encode('utf-8'))
                                       for k, v in headers.items())

        message = self._message._replace(**dct)

        return self.__class__(
            message,
            self._payload,
            self._protocol,
            self._writer,
            self._time_service,
            self._task,
            secure_proxy_ssl_header=self._secure_proxy_ssl_header) 
Example #21
Source File: payload.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def set_content_disposition(self, disptype, quote_fields=True, **params):
        """Sets ``Content-Disposition`` header.

        :param str disptype: Disposition type: inline, attachment, form-data.
                            Should be valid extension token (see RFC 2183)
        :param dict params: Disposition params
        """
        if self._headers is None:
            self._headers = CIMultiDict()

        self._headers[hdrs.CONTENT_DISPOSITION] = content_disposition_header(
            disptype, quote_fields=quote_fields, **params) 
Example #22
Source File: payload.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def __init__(self, value, *, headers=None, content_type=sentinel,
                 filename=None, encoding=None, **kwargs):
        self._value = value
        self._encoding = encoding
        self._filename = filename
        if headers is not None:
            self._headers = CIMultiDict(headers)
            if content_type is sentinel and hdrs.CONTENT_TYPE in self._headers:
                content_type = self._headers[hdrs.CONTENT_TYPE]

        if content_type is sentinel:
            content_type = None

        self._content_type = content_type 
Example #23
Source File: chromerdp.py    From chrome-prerender with MIT License 5 votes vote down vote up
def _on_request_will_be_sent(self, obj: Dict) -> None:
        document_url = obj['params']['documentURL']
        redirect = obj['params'].get('redirectResponse')
        if not redirect and document_url[len(self._url):] == '/':
            redirect = {'url': self._url, 'headers': {'location': document_url}}
        self._last_active_time = time.time()
        if not redirect and document_url == self._url:
            self._requests_sent += 1
        elif not redirect and document_url != self._url and self._requests_sent == 0:
            # https://www.baidu.com Chrome navigate to https://www.baidu.com/
            self._url = document_url
            self._requests_sent += 1
        else:
            if redirect and redirect['url'] == self._url:
                self._url = CIMultiDict(redirect['headers'])['location'] 
Example #24
Source File: test_prettier.py    From python-devtools with MIT License 5 votes vote down vote up
def test_cimultidict():
    v = pformat(CIMultiDict({'a': 1, 'b': 2}))
    assert set(v.split('\n')) == {
        "<CIMultiDict({",
        "    'a': 1,",
        "    'b': 2,",
        "})>",
    } 
Example #25
Source File: forwarder.py    From proxy_tower with MIT License 5 votes vote down vote up
def _gen_headers(r):
    res_headers = CIMultiDict()
    if 'Set-Cookie' in r.headers:
        for cookie in r.headers.getall('Set-Cookie'):
            res_headers.add('Set-Cookie', cookie)
    res_headers['Via-Proxy'] = str(r.proxy)
    return res_headers 
Example #26
Source File: server.py    From gd.py with MIT License 5 votes vote down vote up
def generate_icons(request: web.Request) -> web.Response:
    query = multidict.CIMultiDict(request.query)

    color_1 = color_from_hex(query.pop("color_1", "0x00ff00"))
    color_2 = color_from_hex(query.pop("color_2", "0x00ffff"))
    glow_outline = str_to_bool(query.pop("glow_outline", "false"))
    error_on_not_found = str_to_bool(query.pop("error_on_not_found", "false"))

    settings = f"color_1={color_1}$color_2={color_2}$glow_outline={glow_outline}".lower()
    types = "$".join(f"{key}={value}".lower() for key, value in query.items())
    name = f"[{settings}]({types}).png"
    path = ROOT_PATH / name

    if path.exists():
        return web.FileResponse(path)

    images = [
        await gd.utils.run_blocking_io(
            gd.factory.generate,
            icon_type=gd.IconType.from_value(icon_type),
            icon_id=int(icon_id),
            color_1=color_1,
            color_2=color_2,
            glow_outline=glow_outline,
            error_on_not_found=error_on_not_found,
        )
        for icon_type, icon_id in query.items()
    ]

    if not images:
        raise Warning("No types were generated.")

    image = await gd.utils.run_blocking_io(gd.icon_factory.connect_images, images)

    image.save(path)

    request.loop.create_task(delete_after(5, path))

    return web.FileResponse(path) 
Example #27
Source File: message.py    From aiosip with Apache License 2.0 5 votes vote down vote up
def __init__(self,
                 headers=None,
                 payload=None,
                 from_details=None,
                 to_details=None,
                 contact_details=None,
                 ):

        if headers:
            self.headers = headers
        else:
            self.headers = CIMultiDict()

        if from_details:
            self._from_details = from_details
        elif 'From' not in self.headers:
            raise ValueError('From header or from_details is required')

        if to_details:
            self._to_details = to_details
        elif 'To' not in self.headers:
            raise ValueError('To header or to_details is required')

        if contact_details:
            self._contact_details = contact_details

        self._payload = payload
        self._raw_payload = None

        if 'Via' not in self.headers:
            self.headers['Via'] = 'SIP/2.0/%(protocol)s ' + \
                utils.format_host_and_port(self.contact_details['uri']['host'],
                                           self.contact_details['uri']['port']) + \
                ';branch=%s' % utils.gen_branch(10) 
Example #28
Source File: utils.py    From BentoML with Apache License 2.0 5 votes vote down vote up
def parsed_headers(self):
        return CIMultiDict(
            (hk.decode("latin1").lower(), hv.decode("latin1"))
            for hk, hv in self.headers or tuple()
        ) 
Example #29
Source File: client_reqrep.py    From Galaxy_Plugin_Bethesda with MIT License 5 votes vote down vote up
def update_auto_headers(self, skip_auto_headers: Iterable[str]) -> None:
        self.skip_auto_headers = CIMultiDict(
            (hdr, None) for hdr in sorted(skip_auto_headers))
        used_headers = self.headers.copy()
        used_headers.extend(self.skip_auto_headers)  # type: ignore

        for hdr, val in self.DEFAULT_HEADERS.items():
            if hdr not in used_headers:
                self.headers.add(hdr, val)

        if hdrs.USER_AGENT not in used_headers:
            self.headers[hdrs.USER_AGENT] = SERVER_SOFTWARE 
Example #30
Source File: http.py    From tomodachi with MIT License 5 votes vote down vote up
def resolve_response(value: Union[str, bytes, Dict, List, Tuple, web.Response, Response], request: Optional[web.Request] = None, context: Dict = None, status_code: Optional[Union[str, int]] = None, default_content_type: Optional[str] = None, default_charset: Optional[str] = None) -> web.Response:
    if not context:
        context = {}
    if isinstance(value, Response):
        return value.get_aiohttp_response(context, default_content_type=default_content_type, default_charset=default_charset)
    if isinstance(value, web.FileResponse):
        return value

    status = int(status_code) if status_code else (request is not None and request._cache.get('error_status_code', 200)) or 200
    headers = None
    if isinstance(value, dict):
        body = value.get('body')
        _status = value.get('status')  # type: Optional[SupportsInt]
        if _status and isinstance(_status, (int, str, bytes)):
            status = int(_status)
        _returned_headers = value.get('headers')
        if _returned_headers:
            returned_headers = _returned_headers  # type: Union[Mapping[str, Any], Iterable[Tuple[str, Any]]]
            headers = CIMultiDict(returned_headers)
    elif isinstance(value, list) or isinstance(value, tuple):
        _status = value[0]
        if _status and isinstance(_status, (int, str, bytes)):
            status = int(_status)
        body = value[1]
        if len(value) > 2:
            returned_headers = value[2]
            headers = CIMultiDict(returned_headers)
    elif isinstance(value, web.Response):
        return value
    else:
        if value is None:
            value = ''
        body = value

    return Response(body=body, status=status, headers=headers, content_type=default_content_type, charset=default_charset).get_aiohttp_response(context)