bittorrentlibtorrentlibtorrent-rasterbar

libtorrent disable dht and lsd in the session


We are creating libtorrent session like this:

ses_settings = lt.session_settings() 
ses_settings.ignore_limits_on_local_network = False
ses_settings.announce_to_all_trackers = True
ses_settings.ssl_listen = 0 
ses = lt.session()
ses.listen_on(LISTEN_ON_RANGE_START, LISTEN_ON_RANGE_END)
ses.set_settings(ses_settings)
ses.set_download_rate_limit(download_rate)
ses.set_upload_rate_limit(upload_rate)

Similar to ssl_listen, we want to disable DHT, LSD, UPnP, NAT-PMP in the libtorrent session. Is there any way to do it ?

Also in the libtorrent manual page its mentioned as:

Configuration options can be updated after the session is started by calling apply_settings(). Some settings are best set before starting the session though, like listen_interfaces, to avoid race conditions. If you start the session with the default settings and then immediately change them, there will still be a window where the default settings apply.

Changing the settings may trigger listen sockets to close and re-open and NAT-PMP, UPnP updates to be sent. For this reason, it's typically a good idea to batch settings updates into a single call.

How to do the batch setting updates in a single call ?

Basically we want to change these default setting fields: enable_lsd, enable_dht, enable_upnp, enable_natpmp and then create a session object with these settings.


Solution

  • the session_settings type and set_settings() function on session are deprecated (and have been for quite a while). The reference documentation online (https://libtorrent.org) is for the most recent stable release, so you won't find them documented there.

    Instead, use settings_pack and apply_settings() on the session. Or even better, pass in your settings pack to the session constructor.

    In the C++ interface, settings_pack is a class with a fairly simple interface, but in the python binding it's just a plain dictionary.

    To set up a settings pack in python, you do this:

    sett = {'enable_lsd': False,
      'enable_dht': False,
      'enable_upnp': False,
      'enable_natpmp': False,
      'listen_interfaces': '0.0.0.0:%s' % LISTEN_ON_RANGE_START,
      'download_rate_limit': download_rate,
      'upload_rate_limit': upload_rate,
      'announce_to_all_tracker': True}
    ses = lt.session(sett)
    # ...
    

    You'll find all the available settings in the reference documentation.