xmlrpc.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """xmlrpclib.Transport implementation
  2. """
  3. # The following comment should be removed at some point in the future.
  4. # mypy: disallow-untyped-defs=False
  5. import logging
  6. from pip._vendor import requests
  7. # NOTE: XMLRPC Client is not annotated in typeshed as on 2017-07-17, which is
  8. # why we ignore the type on this import
  9. from pip._vendor.six.moves import xmlrpc_client # type: ignore
  10. from pip._vendor.six.moves.urllib import parse as urllib_parse
  11. logger = logging.getLogger(__name__)
  12. class PipXmlrpcTransport(xmlrpc_client.Transport):
  13. """Provide a `xmlrpclib.Transport` implementation via a `PipSession`
  14. object.
  15. """
  16. def __init__(self, index_url, session, use_datetime=False):
  17. xmlrpc_client.Transport.__init__(self, use_datetime)
  18. index_parts = urllib_parse.urlparse(index_url)
  19. self._scheme = index_parts.scheme
  20. self._session = session
  21. def request(self, host, handler, request_body, verbose=False):
  22. parts = (self._scheme, host, handler, None, None, None)
  23. url = urllib_parse.urlunparse(parts)
  24. try:
  25. headers = {'Content-Type': 'text/xml'}
  26. response = self._session.post(url, data=request_body,
  27. headers=headers, stream=True)
  28. response.raise_for_status()
  29. self.verbose = verbose
  30. return self.parse_response(response.raw)
  31. except requests.HTTPError as exc:
  32. logger.critical(
  33. "HTTP error %s while getting %s",
  34. exc.response.status_code, url,
  35. )
  36. raise