Python interactive subprocess reading - python

I have an interactive Windows console application that I communicate through subprocess module in my program. So far, the console commands(of the application) that I called from my script did not made any problems because they gave instant results(pipe outputs).
However there are other functionalities I need to use that keeps printing output to the console until it recieves "stop" command. I need to read those outputs, but my application is a GUI and I cannot let any hanging occur, and I don't want to get all the output after stop command is issued, I need to get it realtime, when it is served. I made some research and it appears this is a difficult problem to overcome(especially in Windows), but there are no recent questions about this topic.
I did find this,
Non-blocking read on a subprocess.PIPE in python, looks like what I am looking for.
But the answer seems really low-level for a Python solution and it is old. Are there any better-newer solutions for this problem ?
Thanks in advance.

Related

Prevent exe file from being closed/Crash windows os if a program is closed

I'm trying to achieve something ; I created a exe file, that automatically force shutdown the computer at 11 pm.
I would like to make this script impossible to stop or crash, or even make the entire system crash if the program is closed.
How can i achieve this ?
Note :
I'm on a laptop, running windows 10. I made a python file, and i converted it in an exe file with py installer. Then i created a shortcut to that exe file that run the program with admin rights
If you mark the process as critical Windows will trigger a blue screen crash if that process is stopped/killed.
There is information about how to do this here
Note: Although it is possible to do this, it is not a good idea to do so. For example as suggested by Anders, use a Scheduled Task. Having the system crash could result in information loss, or other unintended consequences.
Create a Windows service. You can deny Administrators trying to stop/pause the service, that should slow them down a little bit.
Or since we are talking about triggering something at a specific time, you might want to use the task scheduler instead.
In any case, you will never fully lock down something like this from an Administrator since they can always take ownership and modify the ACL.

How can I sniff packets using Scapy without any CMD opened?

I'm trying to write a script to sniff some packets and send others according to the sniff.
I also want that script to be completely invisible to the user (so it could only be seen from Task Manager).
I tried changing the script extension to .pyw so it would run with pythonw.exe, and it kind of works. However a PowerShell window opens up and when closed, shuts off the pythonw process as well.
I read some on the web and understood Scapy creates it in order to get access to low level networking requests. I also found code to change it into a regular CMD. However I don't want it to change to another shell type, I want this shell to disappear completely so there's no indication om the desktop that the script is running.
How do I achieve this? Is there a way to request scapy to create this shell as a background process? Or is there a way to move an already running CMD to the background?
Thanks in advance!
PS:
The code to change the PowerShell to CMD is:
POWERSHELL_PROCESS.close()
conf.prog.powershell = None
POWERSHELL_PROCESS.__init__()
After the import, and was found here.
I don’t really understand what you are trying to do
This was a well-known bug in scapy, that opened in some IDE (e.g IDLE), the powershell would be visible. As shown in the link you provided, it has been fixed https://github.com/secdev/scapy/issues/1387 so that the shell now is invisible.
If this is your issue, use the latest github version https://github.com/secdev/scapy/archive/master.zip

PDB strategy for debugging Python/Bottle web application hang

I have a multi-threaded Bottle/Python web application that only occasionally hangs. The source can be downloaded here: https://github.com/whbrewer/spc
I've been trying to figure out what is causing the hang, but since it only happens very occasionally and I have not yet figured out how to reproduce it, I try to spend some time whenever it hangs to discover what is causing the problem. I have tried attaching a debugger to the running process using:
gdb /usr/bin/python -p 32489
but this only gives me the libc traceback such as:
0x00007fd2adbdb0fc in __libc_waitpid (pid=32490, stat_loc=stat_loc#entry=0x7ffd8aac545c,
options=0) at ../sysdeps/unix/sysv/linux/waitpid.c:31
31 return INLINE_SYSCALL (wait4, 4, pid, stat_loc, options, NULL);
From this output, I was able to find a similarly asked question which provided some useful advice, but ultimately did not help solve the problem. However, from reading another related informative post, I discovered that the Python debugger PDB can be used to solve this problem. So, my question is, how would I go about implementing PDB in a Python/Bottle application so when it occasionally hangs, I can get a python traceback?
One thing I have noted, is that when the hang happens, it seems the server thread turns into a zombie process, so it shows a defunc python process, while the other threads seem to continue to run. I have tried to use different web servers with Bottle, such as cherrypy and rocket, but both gave the same result.

Make IPython accept commands from network socket and execute in main thread

I'm trying to hook up my Vim so I can send commands to a running IPython instance. There are scripts for this, but they are outdated. I'm trying to write a new one.
My main stumbling block right now is the proper way to make IPython listen to incoming network connections in the background (i.e. a different thread, other solutions are welcome) and executing the received commands in the main thread. Earlier scripts did not execute commands in the main thread and would crash for instance matplotlib regularly.
I see that twisted provides a ThreadedSelectReactor, but I'm at a loss as to how to use it properly with IPython.
Update
A scenario example would be:
2 Windows open, one is a terminal running IPython, one is Vim where you are editing a python script. You select a line in Vim and hit C-Enter, Vim sends the line to the IPython instance, which executes it and prints the result in the IPython terminal, just as if you had copy/pasted the line over myself.
(Matlab users know how useful this functionality can be.)
Paul Ivanov did this a few months ago, using IPython's zmq interface. It's called vim-ipython.
I get the impression that IPython is or has moved to using zmq as messaging protocol. Atleast when I am running 0.11 version zmq support is available.
Using zmq(zero mq) the whole message passing problem is very much reduced to get your Vim instance to communicate over zmq which as far as I know should not be that hard (zmq is ported to a wide variety of platforms).
Look into this blog: http://ipythonzmq.blogspot.com/
and of course: http://www.zeromq.org/

How do I schedule a Python script to run as long as Windows XP is running?

I wrote a temperature logger Python script and entered it as a scheduled task in Windows XP. It has the following command line:
C:\Python26\pythonw.exe "C:\path\to\templogger.py"
It writes data to a file in local public folder (e.g. fully accessible by all who login locally).
So far, I was able to achieve this objective:
1. Get the task to run even before anyone logs in (i.e. at the "Press Ctrl+Alt+Del" screen)
But I'm having problems with these:
1. When I log in, log out, then log back in, the scheduled task is no longer active. I can no longer see it in the Task Manager's Processes tab. I suspect it closes when I log out.
2. I tried to set the task's "Run As..." property to DOMAIN\my-username and also tried SYSTEM, but problem #1 above still persists.
SUMMARY:
I want my program to be running as long as Windows is active. It should not matter whether anyone has logged in or out.
P.S.
I asked the same question in Super User, and I was advised to write it as a service, which I know nothing about (except starting and stopping them). I hope to reach a wider audience here at SO.
Is it possible to run a Python script as a service in Windows? If possible, how?
http://agiletesting.blogspot.com/2005/09/running-python-script-as-windows.html
Your scenario is exactly the required use case for a service, unfortunately tasks are ill suited for what you are looking to do. That said writing services in python is not a walk in the park either, to ease the pain here is a few links I have perused in the past:
http://agiletesting.blogspot.com/2005/09/running-python-script-as-windows.html
http://mail.python.org/pipermail/python-win32/2008-April/007298.html
I used the second link in particular to create a windows scripts that was then compiled to a executable service with py2exe and installed with SrvAny.

Categories