Exemplo n.º 1
0
 def __init__(self,
              *,
              protocol: str = 'http',
              username: str = None,
              password: str = None,
              host: str = '127.0.0.1',
              port: int = 9091,
              path: str = '/transmission/',
              timeout: Union[int, float] = DEFAULT_TIMEOUT,
              logger=LOGGER):
     if isinstance(logger, logging.Logger):
         self.logger = logger
     else:
         raise TypeError('logger must be instance of `logging.Logger`, '
                         'default: logging.getLogger(\'transmission-rpc\')')
     self._query_timeout = timeout
     url = yarl.URL.build(scheme=protocol,
                          user=username,
                          password=password,
                          host=host,
                          port=port,
                          path=urljoin(path, 'rpc'))
     self.url = str(url)
     self._sequence = 0
     self.session: Session = Session()
     self.session_id = 0
     self.server_version = None
     self.protocol_version: Optional[int] = None
     self._http_session = requests.Session()
     self.get_session()
     self.torrent_get_arguments = get_arguments('torrent-get',
                                                self.rpc_version)
Exemplo n.º 2
0
    def __init__(self,
                 address=None,
                 host=DEFAULT_HOST,
                 port=DEFAULT_PORT,
                 protocol=DEFAULT_PROTOCOL,
                 user=None,
                 username=None,
                 password=None,
                 path=DEFAULT_PATH,
                 timeout=None,
                 logger=LOGGER):
        if isinstance(logger, logging.Logger):
            self.logger = logger
        else:
            raise TypeError('logger must be instance of `logging.Logger`, '
                            'default: logging.getLogger(\'transmission-rpc\')')
        if isinstance(timeout, (int, float)):
            self._query_timeout = float(timeout)
        else:
            self._query_timeout = DEFAULT_TIMEOUT

        # self.auth = None
        if address:
            warnings.warn(
                '`address` argument is deprecated, user `host` instead',
                DeprecationWarning,
            )
            host = address

        if user:
            warnings.warn(
                '`user` argument is deprecated, user `username` instead',
                DeprecationWarning,
            )
            username = user

        auth = ''
        if username and password:
            # self.auth = requests.auth.HTTPBasicAuth(username, password)
            auth = '{}:{}@'.format(username, password)

        self.url = '{protocol}://{auth}{host}:{port}{path}'.format(
            protocol=protocol,
            host=host,
            auth=auth,
            port=port,
            path=path,
        )

        self._sequence = 0
        self.session = None
        self.session_id = 0
        self.server_version = None
        self.protocol_version = None
        self.get_session()
        self.torrent_get_arguments = get_arguments('torrent-get',
                                                   self.rpc_version)
Exemplo n.º 3
0
    def __init__(
        self,
        *,
        protocol: "Literal['http', 'https']" = "http",
        username: str = None,
        password: str = None,
        host: str = "127.0.0.1",
        port: int = 9091,
        path: str = "/transmission/",
        timeout: Union[int, float] = DEFAULT_TIMEOUT,
        logger: logging.Logger = LOGGER,
    ):
        if isinstance(logger, logging.Logger):
            self.logger = logger
        else:
            raise TypeError(
                "logger must be instance of `logging.Logger`, "
                "default: logging.getLogger('transmission-rpc')"
            )
        self._query_timeout: _Timeout = timeout
        import urllib.parse

        username = (
            quote(username or "", safe="$-_.+!*'(),;&=", encoding="utf8")
            if username
            else ""
        )
        password = (
            ":" + quote(password or "", safe="$-_.+!*'(),;&=", encoding="utf8")
            if password
            else ""
        )
        auth = f"{username}{password}@" if (username or password) else ""
        url = urllib.parse.urlunparse(
            (protocol, f"{auth}{host}:{port}", urljoin(path, "rpc"), None, None, None)
        )
        self.url = str(url)
        self._sequence = 0
        self.session: Session = Session(self)
        self.session_id = "0"
        self.server_version: Optional[Tuple[int, int, Optional[str]]] = None
        self.protocol_version: Optional[int] = None
        self._http_session = requests.Session()
        self.get_session()
        self.torrent_get_arguments = get_arguments("torrent-get", self.rpc_version)
Exemplo n.º 4
0
    def __init__(self, address='localhost', port=DEFAULT_PORT, user=None,
                 password=None, timeout=None, logger=LOGGER):
        if isinstance(logger, logging.Logger):
            self.logger = logger
        else:
            raise TypeError('logger must be instance of `logging.Logger`, '
                            'default: logging.getLogger(\'transmission-rpc\')')
        if isinstance(timeout, (int, float)):
            self._query_timeout = float(timeout)
        else:
            self._query_timeout = DEFAULT_TIMEOUT
        url_object = urlparse(address)
        if url_object.scheme == '':
            base_url = 'http://' + address + ':' + str(port)
            self.url = base_url + '/transmission/rpc'
        else:
            if url_object.port:
                self.url = url_object.scheme + '://' + url_object.hostname + \
                           ':' + str(url_object.port) + url_object.path
            else:
                self.url = url_object.scheme + '://' + url_object.hostname + url_object.path
            self.logger.info('Using custom URL "' + self.url + '".')
            if url_object.username and url_object.password:
                user = url_object.username
                password = url_object.password
            elif url_object.username or url_object.password:
                self.logger.warning(
                    'Either user or password missing, not using authentication.')

        self.auth = None
        if user and password:
            self.auth = HTTPBasicAuth(user, password)
        elif user or password:
            self.logger.warning(
                'Either user or password missing, not using authentication.')
        self._sequence = 0
        self.session = None
        self.session_id = 0
        self.server_version = None
        self.protocol_version = None
        self.get_session()
        self.torrent_get_arguments = get_arguments('torrent-get',
                                                   self.rpc_version)
Exemplo n.º 5
0
 def __init__(
     self,
     *,
     protocol: "Literal['http', 'https']" = "http",
     username: str = None,
     password: str = None,
     host: str = "127.0.0.1",
     port: int = 9091,
     path: str = "/transmission/",
     timeout: Union[int, float] = DEFAULT_TIMEOUT,
     logger: logging.Logger = LOGGER,
 ):
     if isinstance(logger, logging.Logger):
         self.logger = logger
     else:
         raise TypeError(
             "logger must be instance of `logging.Logger`, "
             "default: logging.getLogger('transmission-rpc')"
         )
     self._query_timeout: _Timeout = timeout
     url = yarl.URL.build(
         scheme=protocol,
         user=username,
         password=password,
         host=host,
         port=port,
         path=urljoin(path, "rpc"),
     )
     self.url = str(url)
     self._sequence = 0
     self.session: Session = Session(self)
     self.session_id = "0"
     self.server_version: Optional[Tuple[int, int, Optional[str]]] = None
     self.protocol_version: Optional[int] = None
     self._http_session = requests.Session()
     self.get_session()
     self.torrent_get_arguments = get_arguments("torrent-get", self.rpc_version)