launch a python program on a server - python

Edit: Please note that it's our first time working with a server, python, and this is my second website, so I know almost nothing about it
We have a project and we need to put our python code on a server
the problem is, we've tried using cgi to run the code and we have two issues:
the program uses Tkinter, and it looks like we can't use program that use Tkinter on a server
when I try to run a script with a loop (our program is a timer, so it will loop), the page loads forever, it only stops loading when the program ends
can we add this python program on a server?
(also, we use a apache server)

You cannot run a tkinter program on a server via CGI. Tkinter and web technologies are incompatible with each other.
Your observation about a program that runs in a loop via CGI is correct -- the program must finish in order for the CGI mechanism to work. The web server will work until that process is finished, and then return what the program returns.

Related

Keep program running on droplet

I've created a simple program in python3 with selenium to monitor a website. The program works as intended when I run it on my own computer, but as I need to keep it running 24/7, I've put it on a droplet from digital ocean. The program works when I am connected to via ssh, but from the log file I can tell that the program stops as soon as I close the connection. I'm guessing it has something that the problem is that the program is running on the same thread as the connection. My question is: How do I make sure the program runs in the background so it will keep running even after I close the coonection to the droplet?
Try using screen.
It allows you to run programs in separate windows so they run completely independent from each other, and also should keep your program running while not connected.
More info about screen: https://www.gnu.org/software/screen/

How to run python script in the background (Windows) with different parameters?

I'm using a python script named vpngate.py.
This script takes as a parameter a country so when i launch my program i do : python vpngate.py korea (for instance). It allows me to change my IP address, looking to the specified country an available IP address.
When i will launch my program, i will have my IP changed (the program is running).
When i will stop my program, i will get my old IP back. And it's only after stopping my program, that i want to re-launch it in order to have a new IP etc..
I read that from package sys, i can import exit, to stop my program. But my question is : how to programatically plan to launch my program every 2minutes for instance. I need to precise that i'm on windows (so the CRON solution doesn't work), and that's really import that the script is ran in background, because i have other script running in parallel. I hope that i was clear enough. Thank you in advance.
Have you tried putting the code performing the setting of IP in a function and calling it from a python script with time.sleep() to time execution.

sending mouse/keyboard events on remote desktop

So I am trying to set up a Continuous integration environment using Jenkins.
One of the build step requires a series of mouse actions/movements to accomplish a task in Excel. I have already written a python script using the ctypes library to do this.
The script works perfectly fine if I run it either through Jenkins or on the server itself when I am actively logged in to the server using remote desktop connection, but as soon as I minimize/close the connection and then run the script from Jenkins, it seems the mouse events never get executed. Is there something I can add to the script to make this work? Thanks for any help you can provide.

Moving CMD python app to the system tray (windows)

I have an python 2.7 application that I would like to move from the CMD window to the system/notification tray (Windows 7). The python script currently listens for particular TCP messages, and upon receipt, logs them to a file.
I have recently found the following system tray code, from another question answered here:
systrayicon
I've been able to run the systrayicon demo correctly by creating the SysTrayIcon.py module. My problem is, when I try to run my TCP application alongside the systrayicon (with some of the demo code intact), it only runs my application, or the system tray application, not both. More specifically, if I list my app's main(sys.argv) before the systrayicon(), it only runs my app without the tray icon. If I have the systrayicon listed before my app, it runs the system tray demo, until I select "Quit", and it then runs my app.
I am able to get my TCP app to run correctly if I plug it into one of the menu selection functions, for example:
def hello(sysTrayIcon):
main(sys.argv)
But I'd like my app to begin listening when the application is opened, without selecting something from the menu to initiate it.
This is likely a very basic problem, and maybe worded poorly, my apologies.
I think the easiest way for you to accomplish this would be to just wrap your script with a small piece of GUI code using Tkinter or wxPython. Then it can reside in the tray. You will probably need to run your script in a thread as it sounds like a long running process that would block the toolkit's main loop. You might need to add some code to the threading code that allows you to kill your script when the GUI exits, but other than that, it should be pretty easy.

Run Python CGI without waiting

I've seen write-ups but am not sure how to apply this to my example. I want to be able to push a button on my webpage, run a piece of python code -- and within the python code it runs another process (executable). The executable takes time, so I want the webpage to not wait until the executable is done running.
I have seen things on daemon and forks, but am not sure how to implement them. I am running an Apache Server with Python CGI on a Windows 8 machine.
Currently, my python code runs and the executable runs fine, but the web page waits until the exe is done processing before updating with an alert window that the "Request has been submitted". Here is my popen code as it is now:
p = subprocess.Popen(['test_sendemail.exe',cmd],shell=False,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
Again, everything works fine here, but I need the python script to end even though the exe is running so that it can update the webpage and move on. (once the exe is done running, it fires an email off with results -- which is why I don't want to wait on the webpage for it to finish).
Any thoughts? Thanks!

Categories

Resources