Skip to content

Instantly share code, notes, and snippets.

@hax0r31337
Last active December 26, 2024 23:33
Show Gist options
  • Select an option

  • Save hax0r31337/cbc1fab37c85b1c3e82160531bb26b8d to your computer and use it in GitHub Desktop.

Select an option

Save hax0r31337/cbc1fab37c85b1c3e82160531bb26b8d to your computer and use it in GitHub Desktop.
Request NAT-PMP for a publicly accessable port and modify qBittorrent prefs automatically
import natpmp
import threading
import urllib.request
import json
import traceback
BASE_URL = "http://127.0.0.1:11205"
def get_qbittorrent_perfs():
url = BASE_URL + "/api/v2/app/preferences"
contents = urllib.request.urlopen(url).read()
return json.loads(contents)
def set_qbittorrent_perfs(prefs):
url = BASE_URL + "/api/v2/app/setPreferences"
req = urllib.request.Request(url)
req.add_header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
data = urllib.parse.urlencode({"json": json.dumps(prefs)})
urllib.request.urlopen(req, data.encode("utf-8"))
def interval():
v = natpmp.map_port(
natpmp.NATPMP_PROTOCOL_TCP, 0, 0, 60, "10.2.0.1", use_exception=False
)
natpmp.map_port(
natpmp.NATPMP_PROTOCOL_UDP, 0, 0, 60, "10.2.0.1", use_exception=False
)
prefs = get_qbittorrent_perfs()
if prefs["listen_port"] != v.private_port:
print("modifying ports {} => {}".format(prefs["listen_port"], v.private_port))
prefs["listen_port"] = v.private_port
set_qbittorrent_perfs(prefs)
def do_interval():
try:
interval()
except:
traceback.print_exc()
event = threading.Event()
do_interval()
while not event.wait(30):
threading.Thread(target=do_interval).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment