怎么修改模块(内的某个类的方法)会比较优雅?

【问题描述】

具体是transmissionrpc这个模块,在使用Client类的add_torrent方法时,torrent参数填某个站点下的下载链接(torrent文件下载链接,不是磁链),触发了反爬机制403 forbidden(该方法的部分代码贴在了下面)。

查了下文档,似乎只能添加cookies,不能添加headers。添加cookies尝试了一下依然403
(用requests模块的get方法,带上headers参数,下载是正常的)

所以想问一下,用什么方法修改这段代码会比较优雅?

不想修改模块内本身的内容。如果在项目内继承这个类再修改,模块内的各种互相调用问题该怎么解决呢?或者有没有其他的方法?

【相关代码】

def add_torrent(self, torrent, timeout=None, **kwargs):
    """
    Add torrent to transfers list. Takes a uri to a torrent or base64 encoded torrent data in ``torrent``.
    Additional arguments are:

    ===================== ===== =========== =============================================================
    Argument              RPC   Replaced by Description
    ===================== ===== =========== =============================================================
    ``bandwidthPriority`` 8 -               Priority for this transfer.
    ``cookies``           13 -              One or more HTTP cookie(s).
    ``download_dir``      1 -               The directory where the downloaded contents will be saved in.
    ``files_unwanted``    1 -               A list of file id's that shouldn't be downloaded.
    ``files_wanted``      1 -               A list of file id's that should be downloaded.
    ``paused``            1 -               If True, does not start the transfer when added.
    ``peer_limit``        1 -               Maximum number of peers allowed.
    ``priority_high``     1 -               A list of file id's that should have high priority.
    ``priority_low``      1 -               A list of file id's that should have low priority.
    ``priority_normal``   1 -               A list of file id's that should have normal priority.
    ===================== ===== =========== =============================================================

    Returns a Torrent object with the fields.
    """
    if torrent is None:
        raise ValueError('add_torrent requires data or a URI.')
    torrent_data = None
    parsed_uri = urlparse(torrent)
    if parsed_uri.scheme in ['ftp', 'ftps', 'http', 'https']:
        # there has been some problem with T's built in torrent fetcher,
        # use a python one instead
        torrent_file = urlopen(torrent)
        torrent_data = torrent_file.read()
        torrent_data = base64.b64encode(torrent_data).decode('utf-8')
    if parsed_uri.scheme in ['file']:
        filepath = torrent
        # uri decoded different on linux / windows ?
        if len(parsed_uri.path) > 0:
            filepath = parsed_uri.path
        elif len(parsed_uri.netloc) > 0:
            filepath = parsed_uri.netloc
        torrent_file = open(filepath, 'rb')
        torrent_data = torrent_file.read()
        torrent_data = base64.b64encode(torrent_data).decode('utf-8')
    if not torrent_data:
        if torrent.endswith('.torrent') or torrent.startswith('magnet:'):
            torrent_data = None
        else:
            might_be_base64 = False
            try:
                # check if this is base64 data
                if PY3:
                    base64.b64decode(torrent.encode('utf-8'))
                else:
                    base64.b64decode(torrent)
                might_be_base64 = True
            except Exception:
                pass
            if might_be_base64:
                torrent_data = torrent
    args = {}
    if torrent_data:
        args = {'metainfo': torrent_data}
    else:
        args = {'filename': torrent}
    for key, value in iteritems(kwargs):
        argument = make_rpc_name(key)
        (arg, val) = argument_value_convert('torrent-add', argument, value, self.rpc_version)
        args[arg] = val
    return list(self._request('torrent-add', args, timeout=timeout).values())[0]

【实际看到的错误信息】

File "D:\Program Files\Python\Python37\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

阅读 1.4k
1 个回答

好像他这个函数并没有考虑反扒问题?
这个函数代码很长吗,可以都贴上来看看

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题