Search code examples
pythonweb-servicesmonitoringstatuscherrypy

Calling a python web server within a script


I would like to have a web server displaying the status of 2 of my python scripts. These scripts listen for incoming data on a specific port. What I would like to do is have it so that when the script is running the web server will return a HTTP200 and when the script is not running a 500. I have had a look at cherrypy and other such python web servers but I could not get them to run first and then while the web server is running continue with the rest of my code. I would like it so that if the script crashes so does the web server. Or a way for the web server to display say a blank webpage with just a 1 in the HTML if the script is running or a 0 if it is not. Any suggestions?

Thanks in advance.


Solution

  • Actually I was just answering a question moderately similar to this one the idea would be to run script A and have it break off 2 threads running the scripts that you intend and then just have a web page do a:

    import threading, cherrypy
    from cherrypy import expose
    
    class thread1(threading.Thread):
        def run(self):
            #code for script 1 goes here
    
    class thread2(threading.Thread):
        def run(self):
            #code for script 2 goes here
    
    t1 = thread1()
    t2 = thread2()
    
    t1.start()
    t2.start()
    
    @expose
    def check(self):
        if t1.isAlive() and t2.isAlive():
            return "1"
        return "0"
    

    I would advise you to put either nginx or apache infront of this with them being a reverse proxy.

    Now there is 2 ways that this will show you that one of them stopped. Either it will show you a 1 that both are running fine. A zero if one or both stopped but managed to keep the rest of the script running. Or nginx/apache will give you a 500 error saying that the backend server (ie:cherrypy) crashed which means that the entire script stopped working.