How do I spawn a process and ignore all user prompts? - python

I'm trying to automate mounting and unmounting of UNC drives with Python on Windows. I'm using the subprocess module to the execute the various commands so I can log their output. However, the NET USE command occasionally prompts the user for input (like a password). Usually things work well, but since I am using subprocess.communicate(), my program hangs indefinitely when NET USE asks my "subprocess" for input. Of course, I have no idea it's asking for input. It just sits there waiting patiently and making me frustrated.
This is a more general problem than just NET USE, so please don't go that route. There are other instances where some (other) program asks for input and my program freezes. Any thoughts?

Some programs are smart and realize that if there is no way for a user to input anything, then they shouldn't even ask. For such programs, you can add "stdin=open('/dev/null')" to the subprocess.Popen options.
In the more difficult case, where the program really insists on interacting, all you can do is provide the interaction yourself.
EDIT: I should mention that the pexpect library is a great way to do such interaction.

Related

What are run time inputs in python3?

I have been recently been trying out this app called compiler(on android) and whenever I try run code it is asking for something called a runtime input? I am new to coding so I really don't know what it means. Another issue is that when I try to run inputs e.g input("hi: ") I am unable, to type anything in the terminal, wondering if there is any fix? In advance thanks to anyone who responds.For some reasons the staic terminal does not apply to other ides such as pycharm or pydroidenter image description here
This is a common issue in the most of the online ide. You have to provide your input in that alert box and hit run.
The reason it happens is because your code is not running on your device instead running on a remote server which only allocates limited time(3-5 seconds) for your code to execute, so to make full use of that time, you are asked to give all the input beforehand so that program doesn't have to wait for user input and waste that precious time.
Also, you can't expect prompt messages to work here as they are working in an offline ide.

Paramiko: "Must be connected to a terminal."

I am trying to use Paramiko to access the input and output of a program running inside of a screen session. Let's assume there is a screen screenName with a single window running the program I wish to access the I/O of. When I try something like client.exec_command('screen -r screenName') for example, I get the message "Must be connected to a terminal" in the stdout.
Searching around, I learned that for some programs, one needs to request a "pseudo-terminal" by adding the get_pty=True parameter to exec_command. When I try this, my code just hangs, and my terminal becomes unresponsive.
What else can I try to make this work?
To provide a little more context to my problem, I am creating a web app that allows users to perform basic operations that I have previously done only in a PuTTy terminal. A feature I wish to have is a kind of web terminal designed to interact with the input and output of this particular program. Effectively all I want to do is continuously pipe output from the program to my backend, and pipe input to the program.

Possible to run a delayed code execution?

Will it is possible to run a small set of code automatically after a script was run?
I am asking this because for some reasons, if I added this set of code into the main script, though it works, it will displays a list of tab errors (its already there, but it is stating that it cannot find it some sort).
I realized that after running my script, Maya seems to 'load' its own setup of refreshing, along with some plugins done by my company. As such, if I am running the small set of code after my main script execution and the Maya/ plugins 'refresher', it works with no problem. I had like to make the process as automated as possible, all within a script if that is possible...
Thus is it possible to do so? Like a delayed sort of coding method?
FYI, the main script execution time depends on the number of elements in the scene. The more there are, it will takes longer...
Maya has a command Maya.cmds.evalDeferred that is meant for this purpose. It waits till no more Maya processing is pending and then evaluates itself.
You can also use Maya.cmds.scriptJob for the same purpose.
Note: While eval is considered dangerous and insecure in Maya context its really normal. Mainly because everything in Maya is inherently insecure as nearly all GUI items are just eval commands that the user may modify. So the second you let anybody use your Maya shell your security is breached.

How do I write a test for an interactive text-based program?

I'm trying to write a test (using unittest) for a simple Python program. It is an interactive program, specifcally it will be a text-based game, for now basically a port of World of Zuul to Python.
So now I want to do good test-driven development and write a test and make sure that when the user inputs command x, that y results happen. Example: when the user inputs "quit", I want the program to end. I want to ensure that behavior. The only problem is, I can't seem to think of a way to give my own program input without actually typing that input into the shell, so I can't think of how to write a test for this.
Currently, I have embedded the program in its own thread... I was thinking this would allow me to asynchronously give it input, but I still don't see how this can be done.
Is there a way to programmatically and dynamically give my own program input? ... Like, using stdin or something? If so I am thoroughly confused, and would appreciate an example.
Finally, if I am on the wrong track here, please tell me, because I am new to both Python and test-driven development, and I would appreciate any suggestions.
You can set up tests that run the program's main function and read the input that the user would provide from a file (or from a string) if you set up the code like this:
def main(inp):
# play game
if __name__ == '__main__':
main(sys.stdin)
Then the test involves setting up an input stream (perhaps a StringIO), feeding that to main, and monitoring the output. If you think the program might hang, you can run it in a separate thread and give it a timeout.
If you want to get fancy, you can even use a pty to simulate terminal input.

In Python, how could I get a user input while simultaneously running the script?

In my program I am trying to take the chat from a website and printing it on my console. While that's going on I'm using raw_input to get chat from whoever is using it. My problem is that raw_input pauses the rest of the script until i say something or press enter. Is there a simple way to fix this?
You have to multithread. One thread for user input and another for the background tasks.
The documentation is a bit complex (I'm pretty confused by it), but it's a start: http://docs.python.org/library/threading.html
You may also want to look into the curses module: http://docs.python.org/library/curses.html

Categories