File: rpc.py

package info (click to toggle)
python-scrapy 2.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,664 kB
  • sloc: python: 52,028; xml: 199; makefile: 25; sh: 7
file content (40 lines) | stat: -rw-r--r-- 1,178 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
This module implements the XmlRpcRequest class which is a more convenient class
(that Request) to generate xml-rpc requests.

See documentation in docs/topics/request-response.rst
"""

from __future__ import annotations

import xmlrpc.client as xmlrpclib
from typing import Any

import defusedxml.xmlrpc

from scrapy.http.request import Request
from scrapy.utils.python import get_func_args

defusedxml.xmlrpc.monkey_patch()

DUMPS_ARGS = get_func_args(xmlrpclib.dumps)


class XmlRpcRequest(Request):
    def __init__(self, *args: Any, encoding: str | None = None, **kwargs: Any):
        if "body" not in kwargs and "params" in kwargs:
            kw = {k: kwargs.pop(k) for k in DUMPS_ARGS if k in kwargs}
            kwargs["body"] = xmlrpclib.dumps(**kw)

        # spec defines that requests must use POST method
        kwargs.setdefault("method", "POST")

        # xmlrpc query multiples times over the same url
        kwargs.setdefault("dont_filter", True)

        # restore encoding
        if encoding is not None:
            kwargs["encoding"] = encoding

        super().__init__(*args, **kwargs)
        self.headers.setdefault("Content-Type", "text/xml")