rpc.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. """
  2. This module implements the XmlRpcRequest class which is a more convenient class
  3. (that Request) to generate xml-rpc requests.
  4. See documentation in docs/topics/request-response.rst
  5. """
  6. from six.moves import xmlrpc_client as xmlrpclib
  7. from scrapy.http.request import Request
  8. from scrapy.utils.python import get_func_args
  9. DUMPS_ARGS = get_func_args(xmlrpclib.dumps)
  10. class XmlRpcRequest(Request):
  11. def __init__(self, *args, **kwargs):
  12. encoding = kwargs.get('encoding', None)
  13. if 'body' not in kwargs and 'params' in kwargs:
  14. kw = dict((k, kwargs.pop(k)) for k in DUMPS_ARGS if k in kwargs)
  15. kwargs['body'] = xmlrpclib.dumps(**kw)
  16. # spec defines that requests must use POST method
  17. kwargs.setdefault('method', 'POST')
  18. # xmlrpc query multiples times over the same url
  19. kwargs.setdefault('dont_filter', True)
  20. # restore encoding
  21. if encoding is not None:
  22. kwargs['encoding'] = encoding
  23. super(XmlRpcRequest, self).__init__(*args, **kwargs)
  24. self.headers.setdefault('Content-Type', 'text/xml')