LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-18-2012, 08:35 AM   #1
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 262

Rep: Reputation: 5
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.
 
Old 12-18-2012, 10:47 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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?
 
Old 12-18-2012, 09:34 PM   #3
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 262

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by pan64 View Post
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.
 
Old 12-19-2012, 03:05 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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.....
 
1 members found this post helpful.
Old 12-19-2012, 03:59 AM   #5
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 262

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by pan64 View Post
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.
 
Old 12-19-2012, 04:04 AM   #6
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 262

Original Poster
Rep: Reputation: 5
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

Last edited by colucix; 01-16-2013 at 08:18 AM. Reason: Masked credentials.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Python script sending email notifications to only one user kaplan71 Programming 3 10-03-2012 06:54 PM
Python script parse NMEA data from GlobalSat BU-353 buffdigger Linux - Newbie 1 04-07-2010 08:29 PM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 02:56 AM
shell script: sending data through the serial port funkymunky Programming 2 06-10-2004 12:47 AM


All times are GMT -5. The time now is 06:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration