I've found a Python telnet file, which works fine (at binary tides) but I'm hoping to (learn to) implement control over the input area such that I have shell like history and editing of the input line, uninterrupted by the feed from the telnet server I'm logged into.
I've seen some posts on this, would I be right to think that I need to learn threading so that I can simultaneously watch the server, the input line and handle the division of the console area, to do this? Similar to the discussion of threads/sockets here (SO separate threads for sockets..) except that is for Java and I'm trying to study Python.
Or perhaps SO TKinter is what I should put in my study list..?
EDIT: I should mention that I hope to move to GUI presentation eventually for my small projects but for this one I'm happy to use the console...I figured it might be a smaller step (learning curve).
Related
My goal is fairly simple, I have written a python 3 script that runs on an ubuntu server at my workplace. Currently I (and other users) have to SSH into this ubuntu box anytime we want to run the script, which with a growing number of users is getting messy - from a convenience and security standpoint. So my boss would like me to create a nice webpage-based GUI to access this script and some other similar scripts I have written.
This seems like a straight forward thing, but the difficulty is in the details - the script is used for viewing and editing records so it has a lot of back-and-forth communication with the user (using input()) as well as screen clears and code like the following:
sys.stdout.write("\033[F") #back to previous line
sys.stdout.write("\033[K") #clear line
...to keep things clean and within one terminal.
If absolutely needed of course I could modify the script to accept inputs in a different format and output in a more html-friendly method without line and screen clears, but this would be rather tedious to redo for the whole script. SO - my question is what would be the best way to achieve what I'm after with minimal modifications to my written code? Ideally the webpage would just have a container holding a terminal-like graphic that handles all the input and output of the selected script as if it was running through a full fat terminal.
Things I've already looked into include basic python web frameworks such as CGI or mod_wsgi, or a python package like flask, but - correct me if I'm wrong - it seems there isn't a nice way to handle constant inputs within scripts with these let alone the terminal-oriented clearing I do for clearing lines and screens.
I also looked into ways to just host a full terminal on the webpage, and security concerns aside, this seems like the closest solution - using something like ajaxterm (which seems rather outdated now?) or wetty. My main concern with this is it isn't any easier than just using SSH at that point. Unless I could modify the terminal to auto-login and start a specific script automagically with the push of a button on the webpage, this would just be a glorified web-rendition of putty.
Any suggestions on how you would achieve this?
I have something in my clipboard, and I'd like to run a python script that invokes CTRL+V as if it was pressed on the keyboard, and pastes the clipboard's content to the current focused window (say chrome). Any idea how to do that?
You have an X-Y problem.
What you want to accomplish is programmatically take data from one program (where you hit cntrl-V) and place it into another arbitrary program (chrome).
There are two ways to do that:
First
You can either set the programs up to have a data exchange mechanism such as a system pipe, or a network connection. This requires some API for data exchange to be already included in the program or access to the source so you might add one. There are very specific channels for cross program data exchange and you wont do well to try to circumvent them. Program A cant just say
get_program_b().get_text_box().add(clip_board);
That would be a violation of Process Isolaton and an OS like windows is written expressly to make it impossible. Some programs are designed to take input from other programs.
popen.open('mysql -e "ISNERT INTO table (a) VALUES ('4')")
Chrome is not one of those programs, chrome avoids allowing programs from doing this because it would be a target for programs to do things like, get the saved password or credit card data out of chrome. Or use save password to login to someone account and buy things in someone elses name.
Second
You could try to spoof user input and input the data exactly like a user would so chrome wont know the difference. But spoofing a user is hard to do and intentionally so because it prevents malicious scripts from taking control of a computer and doing bad things. The makers of windows are accutely aware that spoofing input is a method to circumvent allowed data exchange channels. So the makers of windows made it hard to do. You need to have access to a lot of system assets that most programs wont be given. At a minimum a program has to run as admin on windows to accomplish this, then there are libs that will let you do it. Even then Im willing to bet there are easier way to get the job done. On a machine where you have access to anything and everything it is possible. If you don't have admin access, it should be downright impossible without knowing some unpatched exploit in the system.
Therefore
What you are trying to do goes against what the computer was designed to let you do. If we had more information on what you want to accomplish maybe some of the wonderful people here could help. Getting to the end result you want shouldnt be that hard. But you way of doing it is like trying to run across the ocean, when you just need a boat. As it is my answer is -- dont do it, that's not how windows was designed to work.
I am working with software that requires only occasional input and that otherwise runs completely in the background.
The input it requires is so simple that I have been able to fully automate it using OpenCV and python. Unfortunately in order to send the input, I need to use mouse events for this, resulting in my mouse being moved around. I tried other ways, such as sending other post_messages directly to the window, but they didn't work unfortunately. So since moving the mouse is the only way to interact, it hinders my productivity, since it will interfere with my daily work. Ideally I would like to use multiple desktops, where the mouse/keyboard inputs are completely separated and where I run all my usual stuff on Desktop 1 and the program & automation on Desktop 2. Doing this seems difficult, however, since there is only one input stream and I would need to have separate input streams which can be controlled individually.
So my question is basically if it is possible to have multiple desktops with independent inputs that can be automated separately?
Please note:
I know there are some solutions that provide input for two mice, but I haven't seen any info on how to interface with them (from python for example), and a lot of them seem to be buggy or discontinued.
I also know that I can use a virtual machine, but unfortunately that is a bit of an overkill, especially given that the underlying program that I want to automate requires quite a bit of resources and those are better used within my primary system.
(I am using Python and ArchLinux)
I am writing a simple AI in Python as a school project. Because it is a school project, and I would like to visibly demonstrate what it is doing, my intention is to have a different terminal window displaying printed output from each subprocess- one terminal showing how sentences are being parsed, one showing what pyDatalog is doing, one for the actual input-output chat, etc, possibly on two monitors.
From what I know, which is not much, a couple of feasible ways to go about this are threading each subprocess and figuring out display from there, or writing/using a library which allows me to make and configure my own windows.
My question is, then, are those the best ways, or is there an easy way to output to multiple terminals simultaneously. Also, if making my own windows (and I'm sorry if my terminology is wrong when I say 'making my own windows'. I mean building my own output areas in Python) is the best option, I'm looking for which library I should use for that.
So you could go in multiple directions with this. You could create a Tkinter (or GUI library of your choice) output text box and write to that. This option would give you the most control over how to display your data.
Another option is to access multiple terminals via named pipes. This involves spawning an xterm with a pipe and pumping output to it to be written on screen. See this question for and example:
Using Python's Subprocess to Display Output in New Xterm Window
I like #ebarr's answer, but a quick and dirty way to do it is to write to several files. You can then open multiple terminals and tail the files.
I have 2 questions, so I figured I would cram them into 1 single post instead of filling the board up with useless information
Simple description of situation: I am attempting to create a python script that opens an executable for a simple C++ program with an unknown number of inputs in a windows environment, sends some data into that program, and then check to see if it has crashed / rinse and repeat.
Question 1: This is a pipes question. Bear with me, I am still learning about pipes, so I may have a misunderstanding of exactly how they work. Forgive me if I do. Is it possible to detect how many inputs a program has? Basically what I'm attempting to do is open an executable using my python script, that I personally know nothing about, and send in garbage data into each available input. If it is NOT possible to detect how many inputs there are: would there be an adverse reaction (like crashing the program Im sending the data into) if I send more arguments into it than there are inputs? As in the C++ program takes 3 inputs and I send in 6 arguments?
Question 2: Does anyone know if it possible using a python script to detect whether a program has hung or not? So far the best information on this I've been able to find is simply detecting whether the program is running or not via FindWindow, and then I suppose I could monitor the CPU usage to see if it continues to rise... but that is hardly an ideal method (and may not even work properly!) If there are any better known methods out there I would be thrilled!
Thanks for your time :)
An Answer to Question 2
You should look into investigating psutil, hosted # https://github.com/giampaolo/psutil . I don't know whether you'll find what you're looking for, but pusutil is a decent API, offering access to info such as number of CPUs in addition to process information, which is what you want.