I'm learning python and playing with the subprocess module. After reading several different tutorials and descriptions of how to use the module, my understanding was still not incredibly solid so I started working up some simple code to test the various functions and see how everything worked.
I wanted to see how it might behave if I tried to start the same process multiple times, using this code:
import subprocess
def tpltest(x):
while x > 0:
try:
subprocess.Popen('wordgrinder')
except:
print 'something broke!'
x -= 1
x = raw_input('how many?')
tpltest(x)
When I ran this in my terminal, wordgrinder (terminal word processor) opened normally, but then the cursor advanced down the screen at a rate of about one line per second. It appeared to be movement only, rather than any characters being added to the blank file onscreen. I pressed ctrl+c to see if a wordgrinder remained after closing the first instance and was met with my regular command line prompt, but the cursor continued to scroll. I ran top and did not see extra wordgrinder instances open, but the cursor continued scrolling.
Then I discovered the oddest part. If I rolled my mouse scroll wheel up in the window, chunks of text appeared over the window's contents. Some of the chunks were commands I had entered in the console over the past week, some of them were commands I had entered in my python shell but not the regular terminal, and some of the text was my WiFi security information, including the plaintext WPA2 password for my network. Scrolling the mousewheel down had no effect but scrolling up would overwrite a few lines with new text, seemingly from other random places in my system. After closing the terminal window and opening a new one, things were back to normal.
I'm running Debian 8 64bit with XFCE and using the default terminal emulator.
What did my code do, and why?
The explanation that I can offer is that what subprocess does is to run a background process. Additionally, wordgrinder is a program that takes full control of the terminal session by capturing input from the keyboard (stdin) and controlling the command-line (stdout) in a very particular way.
When you ran wordgrinder in the background through your script, it caused some funky things to happen. That's pretty crazy that your WiFi password ended up getting displayed from your experiment.
Related
So I have a sikuli script running which monitors and executes a said action every 10 minutes continuously. However for various reasons sometimes the run is interrupted and there is no way to alert if the script stops running.
So I tried running a python script, which would monitor the window of the sikuli IDE. When the script runs, the window is no longer visible. So if the window is visible again the python script would run a batch file which would trigger the alert required. The following is the script which I made from seeing other examples here in this site:
WindowName = "SikulixIDE 1.1.3 - C:\\Users\\TestUser\\Downloads\\testing2.sikuli"
while True:
try:
if win32ui.FindWindow(None, WindowName):
subprocess.call([r'C:\Users\TestUser\Documents\notification.bat'])
break
except win32ui.error:
#print("its not running!")
continue
The problem I am running into with the above code is, even when the sikuli script is running and the IDE window (one with the WindowName) is not actually visible to me, it still finds it and goes into the if block. I am not sure what's going wrong here, if the window is not visible in Task Manager, FindWindow shouldn't be able to find it, correct?
I would say you are trying to do this in very complicated way.
I will strongly suggest to look at '-r' option of sikuli and to write only one sikuli script that will handle exceptions by itself and will not need monitoring.
In this case if you really need additional monitoring for the script it will be easier since you can look for process with specific command line(instead of visible window)
I am using the python curses library. I am trying to make an asterisk blink using this code win.addstr(6, 4, "*", curses.A_BLINK) However it does not work on gnome terminal. I tried using it on xterm and it works. It also does not work on the recovery shell. How can I make text blink using the curses library or some other method?
You could make a program (whether with curses or even hardcoded) that draws text on the screen and overwrites it with blanks, with a suitable time delay (if it's too short, it annoys people — see PuTTY for an example of that).
The drawback is that it would "blink" only as long as the program runs, and of course it's a little complicated.
As a shell script, you could do this:
save the stty settings,
change the stty settings to prevent output of carriage return (\r) from being converted to \r\n
print the text, ended with \r
wait a while, e.g., sleep 1
print blanks where you wrote text
wait a while
loop back to the first "print"
on exit, restore the stty settings.
For a curses application - you could make it "blink" by replacing the text, in a similar way. For what it's worth, the xmas example in ncurses-examples uses a combination of window copying and terminal blinking for its animation effects (see C blinkit function and Python translation).
I have a program that I'm currently using tkinter to pop up a window and use the root.title of the window as a counter. I call this program through subprocess mutliple times, aka multiple windows pop up and display that I only have to look at the counter to see how long before each of the programs is finished running.
I want to know how long before each and every process is finished. Doing a bit of testing I don't believe using subprocess it will report back to IDLE and show a counter using print(). At least it doesn't appear to be doing so right now for me.
Is there any way of accomplishing this same task without using tkinter?
Below is the link to the other open question right now that I'm currently using to open the tkinter window that closes on me unexpectedly. I'm not completely sure when a windows closes if the whole program stops running or if just the windows closes. Hence why I want the counter...hence why I have always been using a tkinter window as I don't know of any other way of doing this that serves the same purpose.
I want the counter so I can tell the program was finished if my internet connection gets dropped. I don't have internet access at home so I'm always using free-wifi which quite often has timeouts on it. I want to know for sure whether the programs have finished running or if I got timedout and need to rerun the program.
Python program terminating unexpectedly
G'day,
I've just posted this question here. Following on from that, is there a means to lock keyboard user input to the terminal, when it's running behind another window? My system requires a user to scan their barcode (barcode scanner acts as a keyboard. ie. outputs a string of letters and presses enter) inside the terminal. However, the system also requires that a log CSV file be displayed on the attached monitor. As such, with the terminal in the background, the cursor automatically reverts to the log CSV file when opened, which disables the users' barcode scan from being entered into the terminal.
I'm still relatively new to Python, and haven't completely figured out the functionality of this system. I will eventually set it up such that when the system boots, the log file will automatically open on top, with the terminal (and cursor input) running in the background.
Again, I don't have any code to demonstrate my attempts, but I have done extensive research. The only thing I've found that may offer this functionality is xdotool. I could automatically rearrange the windows such that the terminal was always at the back, and somehow automatically allocate the terminal as the 'active' window?
Any help here would be great!
Thanks!
A real newbie question here.
I'm using IDLE 2.7.2 on OSX 10.7.2 and reading Zelle's Python:Programming. I haven't programmed since the 80's, so after going through the command line stuff, I'm excited to dive into Objects. I grab his graphics.py file and copy it into documents (this seems like the default location for IDLE) and start up IDLE.
He then suggests a few commands into the shell:
from graphics import *
win = GraphWin() #which opens a graphics window with no problem
He then goes on to have you draw some lines and shapes in the window. Those graphics show up just fine in the window.
Here's my problem. If I try to mouse over the graphics window, I get the Mac pinwheel. Moving the window doesn't help. So the window seems like it's crashing (though IDLE is doing fine), but strangely, if I keep entering commands into IDLE, the shapes keep drawing normally in the window.
Am I doing something wrong? Is this normal? Thanks,
Henry
Idle runs the python commands you enter in another process, so it's reasonable to expect this "lock up" behavior to be different between the program controlled window and Idle.
What's probably going on, though is that everything is fine, but you have not yet started the event loop in the program you're typing in. The operating system notices that the program is not emptying out its event queue when you mouse over it (which creates lots of events) and it's helpfully informing you (by way of the pinwheel) that the program seems to be busy.
You will likely reach a point later in the tutorial where you do start the event loop, and the pinwheel will magically go away.