使用Python和Libtorrent加载磁力链接停滞问题咨询
Let's break down why your libtorrent script is stuck at 0% even though the magnet link works in other clients, and fix it step by step:
1. Missing Session Configuration for Peer Discovery
Most torrent clients automatically enable features like DHT, UPnP, and port mapping to find peers, but libtorrent's default session might not have these enabled out of the box. Without them, your script can't connect to peers even if the magnet link is valid.
Fix: Add session initialization settings to enable peer discovery tools:
ses = lt.session() # Set a port range for incoming connections ses.listen_on(6881, 6891) # Enable DHT, UPnP, and NAT-PMP to improve peer connectivity ses.start_dht() ses.start_upnp() ses.start_natpmp()
2. Save Path Permissions or Existence
The /downloads/ path is a system-level directory, and your script might not have write permissions there. Even if the path exists, lack of write access will block the download from starting.
Fix: Use a directory your user owns, and ensure it exists before starting the download:
import os save_path = './downloads/' # Local directory instead of system path if not os.path.exists(save_path): os.makedirs(save_path) params = {'save_path': save_path}
3. Insufficient Peer Connection Wait Time
Right after getting metadata, your script starts checking progress, but it might take a few seconds to connect to peers. The 0% progress could just mean no peers are connected yet, not that the download is broken.
Fix: Add peer count info to debug connectivity, and give the script time to find peers:
print 'got metadata, starting torrent download...' while (handle.status().state != lt.torrent_status.seeding): s = handle.status() # Print detailed status to see if peers are connecting print '%d %% done, %d peers connected' % (s.progress*100, s.num_peers) time.sleep(1)
4. Tracker Compatibility Issues
Some trackers block default libtorrent user agents. If your script isn't identifying itself properly, trackers might not send peer lists.
Fix: Add a custom user agent to the params:
params = { 'save_path': save_path, 'user_agent': 'libtorrent/1.1.10' # Use your actual libtorrent version }
Full Modified Script
Putting it all together, here's the updated code that addresses these common issues:
import libtorrent as lt import time import os # Initialize session with peer discovery features ses = lt.session() ses.listen_on(6881, 6891) ses.start_dht() ses.start_upnp() ses.start_natpmp() # Prepare save path save_path = './downloads/' if not os.path.exists(save_path): os.makedirs(save_path) # Add magnet link with custom user agent params = { 'save_path': save_path, 'user_agent': 'libtorrent/1.1.10' } link = "magnet:?xt=urn:btih:bee75372b98077bfd4de8ef03eb33e9289be5cd8&dn=Avengers+Infinity+War+2018+NEW+PROPER+720p+HD-CAM+X264+HQ-CPG&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Fzer0day.ch%3A1337&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969" handle = lt.add_magnet_uri(ses, link, params) print 'downloading metadata...' while (not handle.has_metadata()): time.sleep(1) print 'got metadata, starting torrent download...' # Monitor download with detailed status while (handle.status().state != lt.torrent_status.seeding): s = handle.status() print '%d %% done, %d peers connected, download rate: %d KB/s' % ( s.progress*100, s.num_peers, s.download_rate / 1000 ) time.sleep(1)
Additional Checks
- Verify your libtorrent version: Older versions might have bugs with certain magnet links or trackers. Upgrade to the latest stable release if possible.
- Check for firewall restrictions: Ensure your system firewall isn't blocking the port range (6881-6891) you set for incoming connections.
内容的提问来源于stack exchange,提问作者John Jackson




