Unable to get the "whole" childprocess output when using Popen - python

I'm trying to use a python script to automate an interactive console application.
(it's called BLEConsole and it's a microsoft store app https://www.microsoft.com/en-us/p/bleconsole/9p5662fdq58v , not sure if that's important. )
So I've been using os.popenX and subprocess.popen to launch and interact with it, and it's working fine... mostly !
inp,outp = os.popen4('BLEConsole.exe')
inp.write('ls\n') # this one work
inp.write('open #01\n') # this one don't generate any output for some reason
inp.write('format hex\n') # and it's working again ??
inp.write('quit\n')
print outp.read()
some commands work and print to stdout but some other don't.
they do work (the bluetooth device connects just fine) but i'm just unable to capture any output other than line breaks when executing this specific command. (yes it's supposed to output stuff, it does when I do it manually)
edit : yes I'm checking both stdout and stderr
Also I'm not able to capture the "BLE:" prompt using this method, I don't really need it but I don't understand why.
Thanks for your help

Related

cmd console run in background

Okay so I'm working to get cmd prompt to run code without the user noticing most likely will be creating a list of prompts that determine which command is run. The closest I've come so far is using subprocess which as far as I can tell won't work. So what I've been able to get subprocess to do is allow the command(s) to be written without the cmd console however, it's just using the python console.
The goal I am aiming for is to have buttons a user will click that based on the gathered information will run a code i.e. no internet check the nic via "ping ::1" <- in cmd(but without the cmd console showing up or rather the user needing to do anything (beyond the prompts) and seeing as little as possible). It would then store that information for the rest of the applications use possibly never showing the information to the user as it is expected they will not understand nor will they know what to do with the information. I do need the information stored so if needed it can be accessed by someone who can make use of the information.
import subprocess
subprocess.run('cmd', shell=True)
from here in the python console you are able to type whatever command.
The subprocess.run already executes a terminal command. You don't need to call the cmd. And, it won't pop a screen.
To better undestand, try this:
import subprocess
subprocess.run('echo "Hello World!"', shell=True)
It outputs:
"Hello World!"
CompletedProcess(args='echo "Hello World!"', returncode=0)
It also has options for capturing the output, but for what you are trying to do, this might do.

Running an interactive Python script from a Batch File

I am wanting to run an interactive Python Program from a Batch File. I found the answer to the question "pythonw.exe or python.exe?" helpful, but not all the issues I had in mind were resolved. I decided to experiment using con:.
The following demonstrates the kind of interaction I have achieved:
Microsoft Windows [Version 10.0.18362.476]
(c) 2019 Microsoft Corporation. All rights reserved.
c:\sjt\PY\NEWER>type call_py.bat
#echo off
call python c:\sjt\py\newer\testout.py 1>con: 2>con:
c:\sjt\PY\NEWER>type testout.py
print ("Print works if you can see this.")
strwaiter = raw_input ("raw_input prompt: ")
print ("This string was received by strwaiter in response to the prompt: " + strwaiter)
c:\sjt\PY\NEWER>call_py
Print works if you can see this.
raw_input prompt: Here is my response.
This string was received by strwaiter in response to the prompt: Here is my response.
c:\sjt\PY\NEWER>
I tried running call_py.bat again but with it calling pythonw instead of python, this attempt did not produce the desired result.
Also, during my experimenting, I tried calling python without the redirection of 1 and 2. This, likewise, was unsuccessful.
I attempted to add a comment to the relevant answer to that question, but failed because I do not have the required reputation. I am posting this question instead.
Does my experiment add anything to the answers to that question?
Given that I know nothing about the technical details given in that post, why is it that calling python in my batch file works (with these redirections) but calling pythonw doesn't?
PS See start /? about differences in starting types of exe files.
There are two types of programs in Windows. Graphical and console, or another way of saying it is console and non-console. The difference is that console programs automatically get or inherit (if there is an existing one) a console window. And thus get access to StdIn etc (which don't exist in the non console world).
If a program gets a console or not is controlled by a flag in the program file's header.
Typically console programs act as console programs, but they don't have too. They can have windows if they want.
Typically GUI programs don't do console stuff, but if they want they can attach to their parent's console and act as a console programs.
Programs without any UI are GUI programs. GUI program just means no console. If a program wants windows it has to create them.
To give you an idea. If you were to take Notepads's source code and compile it as a console program (by changing an option in the compiler). When you start it a console window will open (or it will inherit the current one) and the normal Notepad window will open. As Notepad doesn't have any code to interact with a console the console will just sit there.
One use for writing graphical programs and compiling them as console is that you use the console window for debugging information. Also if the program crashes error information is written to the console (rather than some deeply buried Problem Report that takes 50 clicks to get to).
The other thing to note is that Windows fully communicates to programs via a window. Console programs have to have threads to process messages and receive very limited messages mainly about the console closing or the user closed your program.
Windows messages are many. Even programs without a user interface will typically create a hidden window to receive messages (like shutdown, sleep, wallpaper changed, USB drive arrived, close program, etc).
On my computer with 4 apps visible I have a total of 410 windows.
https://winsourcecode.blogspot.com/2019/05/winlistexe-list-open-windows-and-their.html
This is Microsoft's Documentation of processes.
https://learn.microsoft.com/en-us/windows/console/about-character-mode-applications
https://learn.microsoft.com/en-us/windows/win32/procthread/processes-and-threads

How to Get Python Application to Communicate with Terminal

I am curious if it possible to make custom terminal commands for a Python application, for example, if I were to make a slideshow program, one could into the command line INDEPENDENTLY from the program and type
C:\Users\foo> Slides
And get
SLIDES v0.0.0 Example
Example command, example command
And if you typed
start unnamed.slides
The slide program would open up and run slide project “unnamed”.
Please ignore any factual errors in this if it were the actual commands for a program I’m just trying to get a point across.
You might want to take a look at the Argparse library that allows you to have command line options that are easily manageable. You can then use these options to execute certain parts of you program.
All you need to do is use terminal input functions inside a while loop with code that responds to the input accordingly. To put it simply it would be like this:
while True:
user_input = input('command: ')
# do whatever with the user input here.

Python shell issue when using Data Nitro

I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).

Python: Redirect output to several consoles?

I have a main program, in which a user can call a sub-process (to download files) several times. Each time, I call aria2c using subprocess, and it will print the progress to stdin. Of course, it is desirable that the user can see the progress of each download seperately.
So the question is how can I redirect the output of each process to a seperate console window?
I'm a bit confused. Using subprocess.Popen(...) should spawn a new command prompt automatically for each call. What is aria2c? Is it a program you had written in python as well? Is it a 3rd party exe that writes to the command prompt window?
I can help you to redirect all the sub-processes output to the main command prompt, so it can be displayed inline.
Also, maybe you can give a little more detail on what is going on first, so I can understand your trouble a bit better.

Categories

Resources