External control script, listen_ports not updating

Suggestions and discussion of future versions
Post Reply
d-Pixie

External control script, listen_ports not updating

Post by d-Pixie »

Hi there.

I use a VPN service with my torrenting and have written a bash script for controlling openvpn. To complement the bash script I wrote a few python scripts to interact with deluge. First I was happy that the torrents all go paused when the tunnel went down and resumed when it went up. But there were room for improvements like automatically setting the correct listen port (it's random, but easy to calculate on my side, on each connect with my VPN-provider) and set deluged to add torrents in paused mode when not in a tunnel (to avoid unprotected downloading, it's just not safe anymore ;o)

The script I have now (a bit bloated by debug stuff but still short):

Code: Select all

#!/usr/bin/python
from deluge.ui.client import client
from twisted.internet import reactor
import sys

d = client.connect()

def on_connect_success(result):
    def on_set_active(status):
        print status
    def on_set_port(status):
        print status
    def on_get_port(status):
    	print sys.argv[1]," == ",status,"?"
        client.disconnect()
        reactor.stop()
    client.core.set_config({"active_port":sys.argv[1]}).addCallback(on_set_active)
    client.core.set_config({"listen_ports":[sys.argv[1], sys.argv[1]]}).addCallback(on_set_port)
    client.core.get_listen_port().addCallback(on_get_port)

d.addCallback(on_connect_success)

def on_connect_fail(result):
    print "connection to deluge failed"

d.addErrback(on_connect_fail)

reactor.run()
seam to work, partly at least. The ports get updated according to the console UI if I run it in a different terminal and the GTK UI confirms this. It doesn't, however, change the active port as when you press "Apply" in the GTK UI ... As far as I can see from the code of the GTK UI it's doing what I'm doing which leads me to believe that my connection to deluged is not the same as that made by the GTK UI ... I have tried to snoop and sniff the connection info and I cant find what's wrong...

So the gist is that it changes the listen_ports just fine but not the active_port and I cant figure out how to do it ... Any help would be great ...

I'm not a great python programmer (I prefer Ruby myself) but I get around. Most of this is copy paste and some modifications of stuff I found in the forums, docs and by browsing the deluge source code. I might be way off here, there might be better ways to do stuff and I'll happily update the script with any suggestions. Just don't forget the main issue ;o)

My specs:
Ubuntu 10.10 x64
Deluge 1.3
libTorrent 0.15.4.0
GTK and CLI UI
localhost server, but in daemon mode
no special user or server config

Thank you for your time!
johnnyg
Top Bloke
Top Bloke
Posts: 1522
Joined: Sun Oct 28, 2007 4:00 am
Location: Sydney, Australia

Re: External control script, listen_ports not updating

Post by johnnyg »

I don't think you need / should set the config for active port, that's more of a read-only config value.
I rummaged around and found an old script of mine which does something similar and it appears that I also set the random port option to False, so that might be worth trying.
I've attached the script for your perusal.
Also, are you running Deluge 1.3.3 or 1.3.0? (you just said 1.3).

Code: Select all

#!/usr/bin/env python

from deluge.ui.client import client
from twisted.internet import reactor
import deluge.configmanager
from deluge.log import setupLogger
from random import randrange
import sys

setupLogger()
deluge.configmanager.set_config_dir("/etc/deluge")

def set_new_port(result, port):
    print "Setting port to %d" % port
    return client.core.set_config({
        "listen_ports" : [port, port],
        "random_port" : False
    })

def get_torrent_ids(result):
    return client.core.get_session_state()

def on_fail(failure, action):
    print "%s failed: %s" % (action, failure)

def stop(result):
    client.disconnect()
    reactor.stop()

try:
    port = randrange(int(sys.argv[1]), int(sys.argv[2]))
except:
    port = randrange(49152, 65525)

d = client.connect(host="localhost")
d.addCallbacks(set_new_port, on_fail,
               callbackArgs=(port,), errbackArgs=("Connection",))
d.addCallbacks(get_torrent_ids, on_fail, errbackArgs=("Setting new port",))
d.addCallbacks(client.core.force_reannounce, on_fail, errbackArgs=("Getting torrent ids",))
d.addErrback(on_fail, "Forcing reannounce")
d.addBoth(stop)

reactor.run()
d-Pixie

Re: External control script, listen_ports not updating

Post by d-Pixie »

Thanks for the tip. I was pretty sure I had tried something like it before but I tried it again. I modified my code a few times and the current version looks like this:

Code: Select all

#!/usr/bin/python
from deluge.ui.client import client
from twisted.internet import reactor
import sys

d = client.connect()

def on_connect_success(result):
    def on_get_ports(status):
        print "on_get_ports: ",status
        client.disconnect()
        reactor.stop()
    def on_get_port(status):
        client.core.get_config_value("listen_ports").addCallback(on_get_ports)
    	print sys.argv[1]," == ",status,"?"
    def on_torrent_id(ids):
    	print ids
    	client.core.force_reannounce(ids)
    	client.core.get_listen_port().addCallback(on_get_port)
    def on_port_set(void):
        client.core.get_session_state().addCallback(on_torrent_id)
    def on_random_on(void):
        client.core.set_config({"listen_ports":[sys.argv[1], sys.argv[1]], "random_port" : False}).addCallback(on_port_set);
    client.core.set_config({"random_port" : True}).addCallback(on_random_on)

d.addCallback(on_connect_success)

def on_connect_fail(result):
    print "connection to deluge failed"

d.addErrback(on_connect_fail)

reactor.run()
As you can see I have made use of both the random_port "hack" and the force_reannounce that I saw in your code (if that was what did it for you). I also changed the chaining of callbacks to make the calls sequential (is there a better way? my way sucks ;o) However, none of it works for me ;/

What I really miss is something like an apply function ;o) It seams that this should not be needed according to all the code I have seen from the GTK client, just set the config and it should work. But doesn't ;o)

Anyone from the coding team that has some additional insight?

Thank you again for your time ...
Post Reply