Skip to content

Instantly share code, notes, and snippets.

@gubatron
Created November 17, 2014 03:44
Show Gist options
  • Select an option

  • Save gubatron/ce044e57b1b3f172fcc8 to your computer and use it in GitHub Desktop.

Select an option

Save gubatron/ce044e57b1b3f172fcc8 to your computer and use it in GitHub Desktop.
testing_dht_alert.py
import libtorrent
from threading import Thread
import time
ERROR_NOTIFICATION_ALERT_MASK = 1
PORT_MAPPING_NOTIFICATION_ALERT_MASK = 4
DEBUG_NOTIFICATION_ALERT_MASK = 32
DHT_NOTIFICATION_ALERT_MASK = 1024
def dhtAlertListener(s):
while s.is_dht_running():
alert = s.pop_alert()
if alert is not None:
print alert
else:
print "waiting until next alert..."
s.wait_for_alert(30000)
settings = libtorrent.session_settings()
settings.use_dht_as_fallback = True
s = libtorrent.session()
s.set_settings(settings)
# by default only error alerts are posted, unless we set the proper masks
s.set_alert_mask(ERROR_NOTIFICATION_ALERT_MASK | DHT_NOTIFICATION_ALERT_MASK | PORT_MAPPING_NOTIFICATION_ALERT_MASK | DEBUG_NOTIFICATION_ALERT_MASK)
s.listen_on(12345,123455)
s.add_dht_router("router.bittorrent.com",6881)
s.add_dht_router("dht.transmissionbt.com", 6881)
s.start_lsd()
s.start_natpmp()
s.start_upnp()
s.start_dht()
if s.is_dht_running():
print "DHT is running"
t = Thread(target=dhtAlertListener, args=(s,))
t.start()
t.join()
@gubatron
Copy link
Copy Markdown
Author

Sample output:

frostwire@main1:~/tmp$ python main.py 
DHT is running
UPnP: deleting port map: [ protocol: udp ext_port: 50556 local_port: 50556 ]
UPnP: adding port map: [ protocol: udp ext_port: 12345 local_port: 12345 ] 
UPnP: deleting port map: [ protocol: tcp ext_port: 50556 local_port: 50556 ]
UPnP: adding port map: [ protocol: tcp ext_port: 12345 local_port: 12345 ] 
UPnP: deleting port map: [ protocol: tcp ext_port: 4433 local_port: 4433 ]
UPnP: adding port map: [ protocol: tcp ext_port: 4433 local_port: 4433 ] 
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 0 action: delete proto: tcp local: 50556 external: 50556 ttl: 0 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 0 action: delete proto: tcp local: 50556 external: 50556 ttl: 0 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 0 action: delete proto: tcp local: 50556 external: 50556 ttl: 0 ]
waiting until next alert...
UPnP: broadcasting search for rootdevice
UPnP: HTTP method m-search from 72.232.160.178:54308
UPnP: HTTP method m-search from 72.232.160.178:1900
UPnP: HTTP method m-search from 72.232.160.178:1900
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 0 action: delete proto: tcp local: 50556 external: 50556 ttl: 0 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 0 action: delete proto: tcp local: 50556 external: 50556 ttl: 0 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 0 action: delete proto: tcp local: 50556 external: 50556 ttl: 0 ]
waiting until next alert...
UPnP: broadcasting search for rootdevice
UPnP: HTTP method m-search from 72.232.160.178:54308
UPnP: HTTP method m-search from 72.232.160.178:1900
UPnP: HTTP method m-search from 72.232.160.178:1900
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 0 action: delete proto: tcp local: 50556 external: 50556 ttl: 0 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 0 action: delete proto: tcp local: 50556 external: 50556 ttl: 0 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
UPnP: broadcasting search for rootdevice
UPnP: HTTP method m-search from 72.232.160.178:54308
UPnP: HTTP method m-search from 72.232.160.178:1900
UPnP: HTTP method m-search from 72.232.160.178:1900
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
UPnP: broadcasting search for rootdevice
UPnP: HTTP method m-search from 72.232.160.178:54308
UPnP: HTTP method m-search from 72.232.160.178:1900
UPnP: HTTP method m-search from 72.232.160.178:1900
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 1 action: add proto: tcp local: 4433 external: 4433 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
UPnP: broadcasting search for rootdevice
UPnP: HTTP method m-search from 72.232.160.178:54308
UPnP: HTTP method m-search from 72.232.160.178:1900
UPnP: HTTP method m-search from 72.232.160.178:1900
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 2 action: add proto: udp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
UPnP: broadcasting search for rootdevice
UPnP: HTTP method m-search from 72.232.160.178:54308
UPnP: HTTP method m-search from 72.232.160.178:1900
UPnP: HTTP method m-search from 72.232.160.178:1900
waiting until next alert...
NAT-PMP: ==> port map [ mapping: 3 action: add proto: tcp local: 12345 external: 12345 ttl: 3600 ]
waiting until next alert...
incoming dht get_peers: 5b38427707eed0f74965b9f3a450cae485429e59
waiting until next alert...
UPnP: broadcasting search for rootdevice
UPnP: HTTP method m-search from 72.232.160.178:54308
UPnP: HTTP method m-search from 72.232.160.178:1900
UPnP: HTTP method m-search from 72.232.160.178:1900
waiting until next alert...
incoming dht get_peers: 5b3872e38cf7caa26484518a0ea0ea67c854fc79
waiting until next alert...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment