LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Having some trouble sending data from python script to other function (https://www.linuxquestions.org/questions/linux-newbie-8/having-some-trouble-sending-data-from-python-script-to-other-function-4175441959/)

Adol 12-18-2012 07:35 AM

Having some trouble sending data from python script to other function
 
Hello,

I'm trying to send the output of a python script to another command. I am trying to do this within the python script itself but can also write a bash script if someone can direct me to the commands I should be using.

This is the part of the python script Im having trouble with. Im trying to push the printed number to the qbus command.

Code:

# Now either print out the forwarded port or an error message.
if "port" in resp:
    print resp["port"]
import os
os.system("qdbus org.ktorrent.ktorrent /settings setPort ["port"] : qdbus org.ktorrent.ktorrent /settings apply")

Can anyone see what Im doing wrong with this? The rest of the python script works as it should if I take out the part from import os.

pan64 12-18-2012 09:47 AM

probably this:
Code:

os.system("qdbus org.ktorrent.ktorrent /settings setPort [%s] : qdbus org.ktorrent.ktorrent /settings apply" % ( resp["port"] ))
but I'm not really sure. What is the desired command?

Adol 12-18-2012 08:34 PM

Quote:

Originally Posted by pan64 (Post 4851933)
probably this:
Code:

os.system("qdbus org.ktorrent.ktorrent /settings setPort [%s] : qdbus org.ktorrent.ktorrent /settings apply" % ( resp["port"] ))
but I'm not really sure. What is the desired command?

Thank you but its still not doing what I want.

Its not imputing the result of the python script to the command and my ktorrent port is coming out to 0.

Here is the full python script(which works perfectly without my extra part):

Code:

#!/usr/bin/env python

import urllib
import urllib2
import json
import sys
import netifaces

# put your VPN username and password below
YOUR_USERNAME = "xxxxx"
YOUR_PASSWORD = "xxxxx"

# This should be a long random string, or something that no one else is going
# to be able to guess.  Don't share this amongst multiple computers on the
# same VPN account.  If you have more than one computer, use a different client
# ID for each.  I recommende the output of the Linux "uuidgen" command, but
# you can really just use whatever.
YOUR_CLIENT_ID = "xxxxx"

# The interface your VPN uses.  We use this to look up your local IP, as
# required by the port forwarding API.  Probably you won't have to change this
# unless you really know what you're doing (e.g., if you have more than one
# VPN tunnel on the same system or something)
VPN_IFACE = "tun0"

# This is the API URL.  Don't change this.
API_URL = "https://www.privateinternetaccess.com/vpninfo/port_forward_assignment"

# Look up the local IP from the VPN interface
try:
    local_ip =  netifaces.ifaddresses(VPN_IFACE)[netifaces.AF_INET][0]["addr"]
except ValueError,e:
    print "No such interface.  Are you connected?"
    sys.exit(-1)

# Make the API request to the webserver
request = urllib2.Request(API_URL, urllib.urlencode(
        {
            "user":YOUR_USERNAME,
            "pass":YOUR_PASSWORD,
            "client_id":YOUR_CLIENT_ID,
            "local_ip":local_ip
            }
        ))

# Gather the (possibly multi-line) JSON response and create a python dict
# out of it
response = ""
for line in urllib2.urlopen(request).readlines():
    response += line
resp =  json.loads(response)

# Now either print out the forwarded port or an error message.
if "port" in resp:
    print resp["port"]
    import os
    os.system("qdbus org.ktorrent.ktorrent /settings setPort [%s] ; qdbus org.ktorrent.ktorrent /settings apply" % ( resp["port"] ))
elif "error" in resp:
    print "Error: %s" % resp["error"]
    sys.exit(-1)
else:
    print "Error: no idea what failed!"
    sys.exit(-2)

After I run this python scrip without my line added, I get a number for my port. I can either enter it manualy within ktorrent or use the commands:

Code:

$ qdbus org.ktorrent.ktorrent /settings setPort (number I get from python script)
$ qdbus org.ktorrent.ktorrent /settings apply

thanks to Diantre.

I want to have this command run automatically within the python script.

pan64 12-19-2012 02:05 AM

I see. Try to remove [ and ] around the port number:
Code:

os.system("qdbus org.ktorrent.ktorrent /settings setPort %s; qdbus org.ktorrent.ktorrent /settings apply" % ( resp["port"] ))
also you can try to add echo to test how it works (and modify it if required):
os.system("echo qdbus org.....

Adol 12-19-2012 02:59 AM

Quote:

Originally Posted by pan64 (Post 4852578)
I see. Try to remove [ and ] around the port number:
Code:

os.system("qdbus org.ktorrent.ktorrent /settings setPort %s; qdbus org.ktorrent.ktorrent /settings apply" % ( resp["port"] ))
also you can try to add echo to test how it works (and modify it if required):
os.system("echo qdbus org.....

Thank you soo much. It works perfectly now.

Adol 12-19-2012 03:04 AM

If anyone does a google search for it. This is the python script I am using for Private internet access and Ktorrent to get my ports.

I did not write this python script. It was found on the private internet access help forum but now it will automatically change the port number for Ktorrent.

for the xxxxxx put in your own information and you need netfaces installed with the python bindings.

Code:

#!/usr/bin/env python

import urllib
import urllib2
import json
import sys
import netifaces

# put your VPN username and password below
YOUR_USERNAME = "xxxxx"
YOUR_PASSWORD = "xxxxx"

# This should be a long random string, or something that no one else is going
# to be able to guess.  Don't share this amongst multiple computers on the
# same VPN account.  If you have more than one computer, use a different client
# ID for each.  I recommende the output of the Linux "uuidgen" command, but
# you can really just use whatever.
YOUR_CLIENT_ID = "1981981981981981981212138"

# The interface your VPN uses.  We use this to look up your local IP, as
# required by the port forwarding API.  Probably you won't have to change this
# unless you really know what you're doing (e.g., if you have more than one
# VPN tunnel on the same system or something)
VPN_IFACE = "tun0"

# This is the API URL.  Don't change this.
API_URL = "https://www.privateinternetaccess.com/vpninfo/port_forward_assignment"

# Look up the local IP from the VPN interface
try:
    local_ip =  netifaces.ifaddresses(VPN_IFACE)[netifaces.AF_INET][0]["addr"]
except ValueError,e:
    print "No such interface.  Are you connected?"
    sys.exit(-1)

# Make the API request to the webserver
request = urllib2.Request(API_URL, urllib.urlencode(
        {
            "user":YOUR_USERNAME,
            "pass":YOUR_PASSWORD,
            "client_id":YOUR_CLIENT_ID,
            "local_ip":local_ip
            }
        ))

# Gather the (possibly multi-line) JSON response and create a python dict
# out of it
response = ""
for line in urllib2.urlopen(request).readlines():
    response += line
resp =  json.loads(response)

# Now either print out the forwarded port or an error message.
if "port" in resp:
    print resp["port"]
    import os
    os.system("qdbus org.ktorrent.ktorrent /settings setPort %s; qdbus org.ktorrent.ktorrent /settings apply" % (
resp["port"] ))
elif "error" in resp:
    print "Error: %s" % resp["error"]
    sys.exit(-1)
else:
    print "Error: no idea what failed!"
    sys.exit(-2)

Thank you everyone on LinuxQuestions.org


All times are GMT -5. The time now is 12:19 PM.