{"id":6373,"date":"2018-01-12T10:55:53","date_gmt":"2018-01-12T02:55:53","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6373"},"modified":"2018-01-12T11:11:21","modified_gmt":"2018-01-12T03:11:21","slug":"%e7%94%a8python%e8%bf%9b%e8%a1%8clibtorrent%e5%bc%80%e5%8f%91%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6373.html","title":{"rendered":"\u7528python\u8fdb\u884clibtorrent\u5f00\u53d1\u5b9e\u8df5"},"content":{"rendered":"<h1>\u7f16\u8bd1libtorrent\u7684python bindings<\/h1>\n<p>\u5b89\u88c5\u73af\u5883<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install python-dev\r\nsudo apt-get install build-essential\r\nsudo apt-get install libboost-all-dev \r\nsudo apt-get install libssl-dev\r\n<\/pre>\n<p>\u518d\u4e0b\u8f7d\u6e90\u7801\u7f16\u8bd1<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nwget &quot;https:\/\/github.com\/arvidn\/libtorrent\/releases\/download\/libtorrent-1_1_6\/libtorrent-rasterbar-1.1.6.tar.gz&quot;\r\ntar vxzf libtorrent-rasterbar-1.1.6.tar.gz\r\ncd libtorrent-rasterbar-1.1.6\/\r\n.\/configure --enable-python-binding\r\nmake -j2\r\n<\/pre>\n<p>\u6700\u540e\u751f\u6210\u9700\u8981\u7684\u4e09\u4e2aso\u6587\u4ef6\u5728<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nbindings\/python\/build\/lib.linux-x86_64-2.7\/libtorrent.so\r\nsrc\/.libs\/libtorrent-rasterbar.so\r\nsrc\/.libs\/libtorrent-rasterbar.so.9\r\nsrc\/.libs\/libtorrent-rasterbar.so.9.0.0\r\n<\/pre>\n<p>\u628a\u8fd9\u4e09\u4e2aso\uff08\u5176\u4e2d\u524d\u4e24\u4e2a\u662f\u8f6f\u94fe\uff0c\u8fde\u63a5\u5230\u6700\u540e\u4e00\u4e2a\u6587\u4ef6\uff09\u653e\u5230\u4e00\u4e2a\u6587\u4ef6\u5939\u6bd4\u5982 \/home\/kyle\/libtorrent<\/p>\n<p>\u7136\u540e\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:\/home\/kyle\/libtorrent<\/p>\n<p>\u4e4b\u540e\u76f4\u63a5\u5728python\u4e2d import libtorrent \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<h1>\u505a\u79cd<\/h1>\n<p>\u5047\u8bbe\u6211\u6709\u4e24\u4e2a\u6587\u4ef6 1.mp4 1.txt \uff0c\u5b58\u653e\u5728\u76ee\u5f55 \/home\/kyle\/bt\/seed\/ \u4e0b\u9762\uff0c\u9700\u8981\u505a\u79cd\u3002\u90a3\u4e48\u751f\u6210\u79cd\u5b50\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport sys\r\nimport os\r\nimport libtorrent\r\nimport time\r\n\r\ndir_path = '\/home\/kyle\/bt\/seed' # \u4e0b\u9762\u6709\u4e24\u4e2a\u6587\u4ef6 1.mp4 1.txt\r\nfs = libtorrent.file_storage()\r\nlibtorrent.add_files(fs, dir_path)\r\ntorrent = libtorrent.create_torrent(fs, 0, 4 * 1024 * 1024)\r\n# \u4f7f\u7528\u4e00\u4e2atracker server\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5df1\u642d\u5efa\u4e00\u4e2aserver\r\ntorrent.add_tracker(&quot;http:\/\/tracker.opentrackr.org:1337\/announce&quot;)\r\ntorrent.set_creator('libtorrent %s' % libtorrent.version)\r\nparent_dir = os.path.dirname(dir_path)\r\nlibtorrent.set_piece_hashes(torrent, parent_dir, lambda x: sys.stderr.write(str(x)))\r\nraw = torrent.generate()\r\nprint ''\r\n# \u751f\u6210\u78c1\u529b\u94fe\u63a5\r\nprint libtorrent.make_magnet_uri(libtorrent.torrent_info(raw))\r\nwith open(&quot;\/home\/kyle\/bt\/1.torrent&quot;, &quot;wb&quot;) as f:\r\n    f.write(libtorrent.bencode(raw))\r\n<\/pre>\n<p>\u5176\u4e2dadd_tracker\u65b9\u6cd5\u6dfb\u52a0\u7684tracker\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u4ece\u7f51\u4e0a\u627e\u53ef\u7528\u7684\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5df1\u642d\u5efa\u3002<\/p>\n<p>\u7f51\u4e0a\u627e\u7684\u8bdd\uff0c\u8fd9\u91cc\u6709\u4e00\u4e9b tracker server \u5217\u8868\uff1ahttps:\/\/github.com\/ngosang\/trackerslist<\/p>\n<p>\u81ea\u5df1\u642d\u5efa tracker server \uff0c\u53ef\u4ee5\u7528\u5f00\u6e90\u8f6f\u4ef6\uff1a https:\/\/github.com\/chihaya\/chihaya<\/p>\n<p>\u6216\u8005\u5176\u5b83\u7684, Comparison of BitTorrent tracker software: https:\/\/en.wikipedia.org\/wiki\/Comparison_of_BitTorrent_tracker_software<\/p>\n<h1>\u4e0b\u8f7d\u6587\u4ef6<\/h1>\n<p>\u901a\u8fc7\u79cd\u5b50\u6587\u4ef6\u4e0b\u8f7d\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport sys\r\nimport os\r\nimport libtorrent\r\nimport time\r\n\r\nsession = libtorrent.session()\r\nsession.set_alert_queue_size_limit(1024 * 1024)\r\nsession.listen_on(6881, 6881)\r\ntorrent_info = libtorrent.torrent_info('\/home\/kyle\/bt\/1.torrent')\r\nadd_params = {\r\n    'save_path': '\/home\/kyle\/bt\/',\r\n    'storage_mode': libtorrent.storage_mode_t.storage_mode_sparse,\r\n    'paused': False,\r\n    'auto_managed': True,\r\n    'ti': torrent_info,\r\n    # \u5982\u679c\u662f\u78c1\u529b\u94fe\u63a5\uff0c\u5c31\u7528 url \u66ff\u4ee3 ti \u53c2\u6570\r\n    # 'url': 'magnet:?xt=urn:btih:69bda373e8e3e0c59428bfb90c5df8c50603d751&amp;amp;tr=udp:\/\/9.rarbg.to:2710\/announce',\r\n}\r\nhandle = session.add_torrent(add_params)\r\nsession.start_dht()\r\n\r\nalert = session.pop_alert()\r\nwhile alert:\r\n    print '&#x5B;alert msg]: %s' % alert.message()\r\n    alert = session.pop_alert()\r\nstate_str = &#x5B;'queued', 'checking', 'downloading metadata', \\\r\n            'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']\r\n\r\n# \u963b\u585e\u4e0b\u8f7d\r\nwhile not (handle.is_seed()):\r\n    s = handle.status()\r\n    print '\\ractive_time: %d, %.2f%% complete (down: %.1f kb\/s up: %.1f kB\/s peers: %d, seeds: %d) %s' % \\\r\n            (s.active_time, s.progress * 100, s.download_rate \/ 1000, s.upload_rate \/ 1000, \\\r\n            s.num_peers, s.num_seeds, state_str&#x5B;s.state]),\r\n    sys.stdout.flush()\r\n    time.sleep(0.2)\r\n# \u4e0b\u8f7d\u5b8c\u540e\u5f00\u59cb\u505a\u79cd\r\nhandle.super_seeding()\r\n\r\n# \u963b\u585e\u8f93\u51fa\u505a\u79cd\u72b6\u6001\r\nseedtime = 1000\r\nwhile seedtime &amp;gt; 0:\r\n    seedtime -= 1\r\n    s = handle.status()\r\n    print '\\rseed_time: %d, %.2f%% complete (down: %.1f kb\/s up: %.1f kB\/s peers: %d, seeds: %d) %s' % \\\r\n            (s.active_time, s.progress * 100, s.download_rate \/ 1000, s.upload_rate \/ 1000, \\\r\n            s.num_peers, s.num_seeds, state_str&#x5B;s.state]),\r\n    sys.stdout.flush()\r\n    time.sleep(1)\r\n\r\n<\/pre>\n<p>\u5982\u679c\u7b2c\u4e00\u6b21\u4e0b\u8f7d\uff0c\u5c31\u5728\u751f\u6210\u79cd\u5b50\u7684\u540c\u4e00\u53f0\u673a\u5668\u4e0a\uff0c\u5219save_path\u53c2\u6570\u8bbe\u7f6e\u6210\u505a\u79cd\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u90a3\u4e48\u4e0b\u8f7d\u5f88\u5feb1-2\u79d2\u5c31\u5b8c\u6210\u4e86\uff0c\u7136\u540e\u672c\u673a\u5c31\u8fdb\u5165\u505a\u79cd\u72b6\u6001\u3002<\/p>\n<p>\u8fd9\u65f6\u5019\u5728\u5176\u5b83\u673a\u5668\u4e0a\u6253\u5f00BT\u4e0b\u8f7d\u8f6f\u4ef6\uff0c\u5982\u8fc5\u96f7\uff0cdeluge\u7b49\uff0c\u5c31\u53ef\u4ee5\u6b63\u5e38\u4e0b\u8f7d\u3002\u6216\u8005\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\u8fdb\u884c\u4e0b\u8f7d\u3002<\/p>\n<h1>\u53c2\u8003<\/h1>\n<p>python\u5b9e\u73b0p2p\u6587\u4ef6\u4f20\u8f93\u2014\u9002\u7528\u5927\u6587\u4ef6\u4f20\u8f93\uff1a\u00a0http:\/\/www.361way.com\/python-p2p\/4737.html<\/p>\n<p>Converting a Magnet Link into a Torrent\uff1ahttp:\/\/danfolkes.com\/2012\/04\/19\/converting-a-magnet-link-into-a-torrent\/<\/p>\n<p>libtorrent\u00a0reference documentation\uff1ahttps:\/\/libtorrent.org\/reference.html<\/p>\n<p>Build libtorrent from source\uff1ahttp:\/\/dev.deluge-torrent.org\/wiki\/Building\/libtorrent<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u8bd1libtorrent\u7684python bindings \u5b89\u88c5\u73af\u5883 sudo apt-get install  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6373","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6373"}],"version-history":[{"count":5,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6373\/revisions"}],"predecessor-version":[{"id":6378,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6373\/revisions\/6378"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}