如何在libtorrent中应用代理?

时间:2016-08-21 20:03:27

标签: python proxy libtorrent

我正在创建一个torrent客户端,并希望为其添加代理。我似乎无法让它工作。我正在使用" http://ipmagnet.services.cbcdn.com"检查我的客户端向同行和跟踪器分发的IP地址。如何修复我的代码以正确地将我的代理应用于客户端?

import libtorrent as lt
import time
import os

ses = lt.session()
ses.listen_on(6881, 6891)
r = lt.proxy_settings()
r.proxy_hostnames = True
r.proxy_peer_connections = True
r.hostname = "*myproxyinfo*"
r.username = "*myproxyinfo*"
r.password = "*myproxyinfo*"
r.proxy_port = 1080
r.proxy_type = lt.proxy_type().socks5_pw
#print lt.proxy_type().socks5_pw
ses.set_dht_proxy(r)
ses.set_peer_proxy(r)
ses.set_tracker_proxy(r)
ses.set_web_seed_proxy(r)
ses.set_proxy(r)
t = ses.settings()
t.force_proxy = True
t.proxy_hostnames = True
t.proxy_peer_connections = True
t.proxy_tracker_connections = True
#t.anonymous_mode = True
#ses.set_settings(t)
#print ses.get_settings()
ses.dht_proxy()
ses.peer_proxy()
ses.tracker_proxy()
ses.web_seed_proxy()
ses.proxy()
ses.set_settings(t)


magnet_link = "magnet:?xt=urn:btih:1931ced5c4e20047091742905f30f8d0b69c9ca9&dn=ipMagnet+Tracking+Link&tr=http%3A%2F%2Fipmagnet.services.cbcdn.com%3A80%2F"

params = {"save_path": os.getcwd() + r"\torrents",
               "storage_mode": lt.storage_mode_t.storage_mode_sparse,
              "url": magnet_link}
h = ses.add_torrent(params)

s = h.status()
while (not s.is_seeding):
        s = h.status()

        state_str = ['queued', 'checking', 'downloading metadata', \
            'downloading', 'finished', 'seeding', 'allocating']
        print '%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d)         %s' % \
            (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
            s.num_peers, state_str[s.state])

    time.sleep(1)

2 个答案:

答案 0 :(得分:0)

我建议您查看会话生成的警报,以查看是否有任何内容未能使用代理。

默认情况下,libtorrent认为代理是尽力而为的。如果因任何原因失败,libtorrent将退回尝试直接连接。

如果您想强制使用代理,如果代理失败则失败,请将force_proxy设置为true。

答案 1 :(得分:0)

花了我几个小时找出答案,这对我有用

ses = lt.session()
r = lt.proxy_settings()
r.hostname = "*myproxyinfo*"
r.username = "*myproxyinfo*"
r.password = "*myproxyinfo*"
r.port = 1080
r.type = lt.proxy_type_t.socks5_pw
ses.set_peer_proxy(r)
ses.set_web_seed_proxy(r)
ses.set_proxy(r)
t = ses.settings()
t.force_proxy = True
t.proxy_peer_connections = True
t.anonymous_mode = True
ses.set_settings(t)
print(ses.get_settings())
ses.peer_proxy()
ses.web_seed_proxy()
ses.set_settings(t)

我将r.proxy_port更改为r.port,并将r.proxy_type = lt.proxy_type().socks5_pw更改为r.type = lt.proxy_type_t.socks5_pw

我已经确认可以使用ipmagnet