How to store output of hcitool lescan? [duplicate] - python-2.7

This question already has answers here:
Running shell command and capturing the output
(21 answers)
Closed 6 years ago.
I was trying to scan ble devices using hcitool lescan in a python code. The hcitool lescan works well on the command line but fails to return any output using subprocess.Popen.The code works fine when lescan is replaced with 'scan' ie scan of conventional bluetooth.
My code is:
import os
import time
import subprocess
proc = subprocess.Popen(['sudo','timeout', '20s','hcitool', 'lescan'],stdout=subprocess.PIPE)
proc.wait()
lines = proc.stdout.readlines()
print lines

Have you tried to use communicate?
proc = subprocess.Popen(...)
stdout, stderr = proc.communicate()

Use
from commands import getoutput as shell
s = shell('hcitool scan')
s is a string with what you are looking for.

Related

How to output to command line when running one python script from another python script

I have multiple python scripts, each with print statements and prompts for input. I run these scripts from a single python script as below.
os.system('python script1.py ' + sys.argv[1])
os.system('python script2.py ' + sys.argv[1]).....
The run is completed successfully, however, when I run all the scripts from a single file, I no longer see any print statements or prompts for input on the run console. Have researched and attempted many different ways to get this to work w/o success. Help would be much appreciated. Thanks.
If I understand correctly you want to run multiple python scripts synchronously, i.e. one after another.
You could use a bash script instead of python, but to answer your question of starting them from python...
Checkout out the subprocess module: https://docs.python.org/3.4/library/subprocess.html
In particular the call method, it accepts a stdin and stdout which you can pass sys.stdin and sys.stdout to.
import sys
import subprocess
subprocess.call(['python', 'script1.py', sys.argv[1]], stdin=sys.stdin, stdout=sys.stdout)
subprocess.call(['python', 'script2.py', sys.argv[1]], stdin=sys.stdin, stdout=sys.stdout)
^
This will work in python 2.7 and 3, another way of doing this is by importing your file (module) and calling the methods in it. The difference here is that you're no longer running the code in a separate process.
subroutine.py
def run_subroutine():
name = input('Enter a name: ')
print(name)
master.py
import subroutine
subroutine.run_subroutine()

How to Show Console Output When Running a C++ code in Python Script

I am trying to Make A U.I that takes the c++ code in the text box and displays the output or the errors. I am unable to Copy the result of the console that arises after compiling the code and running the Code. Also, I want to save the output or the errors in a file.
CODE:
from tkinter import *
import subprocess
root=Tk()
def exect():
global e
entry=e.get("1.0","end-1c")
f=open('sample3.cpp','w')
f.write(entry)
f.close()
#this is where problem is because it does not save the output
subprocess.call(["g++","sample3.cpp"])
subprocess.call("./a.out")
#how do i run this code for sample inputs and then store its output
root.destroy()
root.geometry("600x700")
x=Frame(root)
x.grid()
label=Label(root,text="write code for displaying hello world")
label.grid()
e=Text(root)
button=Button(root,text="submit",command=exect)
button.grid()
e.grid()
root.mainloop()
Also, How do I compile and save a program that takes Inputs?
EDIT: This question also seeks running the program for sample inputs which I did not find in the answer which this question was marked as duplicate to?

execute script with C++ [duplicate]

This question already has answers here:
How do I execute a command and get the output of the command within C++ using POSIX?
(12 answers)
How to assign shell command output to a variable in C language
(4 answers)
Closed 9 years ago.
I want to execute a script through a c++ program and get its output. Presently I am doing
system("./script.sh > out.txt");
But I need a command that get the output to a string, some thing like:
out = system("./script.sh");
printf(out);
I can't read the file out.txt after execute the script because I don't have permission to that. I deployed my c++ program at other framework (boinc) that doesn't give me this permission.
Does anybody have a hint?
Thanks in advance!
Felipe
you can use popen() and then get the output of the command from the pipe opened by popen()
FILE *fp;
fp=popen("./script.sh","r");
and to get your output. you can use fgets() or fread() to read from pipe like you read from a file

get output of system() into a variable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best way to capture stdout from a system() command so it can be passed to another function
In linux to get the current status of a service I wrote this code segment::
char cmd[100];
sprintf(cmd,"service %s status",argv[1]);
system(cmd);
It is running fine and it shows the output on the console like : mysql is running OR mysql is stopped
But I need this console output in a string variable. How can I get 'mysql is running' in a string variable so that I can use this string variable later.
thankx.
If you want to capture output then use popen() rather than system().

how to pass a value to c++ from python and back?

i would like to pass values from python to a c++ program for an encryption from inside a python program and then return the value from there to the python program . how to do it?
If you want to use some existing Unix-style command line utility that reads from stdin and writes to stdout, you can use subprocess.Popen by using Popen.communicate():
import subprocess
p = subprocess.Popen(["/your/app"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = p.communicate(input)[0]
As said msw in the other post, the proper solution is using PyObject.
If you want to have a two-way communication between C++ & Python, Boost Python would be interesting for you. Take a look at website Boost Python,
This post would also be interesting:
How to expose a C++ class to Python without building a module