dimanche 4 juin 2017

How to connect to a running web.py script port/socket

I am new to web frameworks and web development in general (I'm primarily a Python desktop developer), and have been trying out web.py, with some success, but now I'm stuck.

My aim is to leave an application "running" and continuously updating, so that I can retrieve the current state on demand. In the basic example below, I want to start a counter and see the current value when I connect to the running app.

print("Content-type: text/html\n\n")
from threading import Thread
import time
import web

urls = ('/', 'index')

web.app = web.application(urls, globals())
web.app.count = 0


def test():
    while True:
        web.app.count += 1
        print(web.app.count)
        time.sleep(5)


class index:
    def GET(self):
        return web.app.count


if __name__ == '__main__':
    t = Thread(target=test)
    t.daemon = True
    t.start()
    web.app.run()

If I run the app with localhost, it works as expected, I connect to localhost:8080 and the value printed to screen matches the value printed to console. If I upload the script to my web hosting as a cgi script, the app is not running continuously, and runs each time I connect to its url, returning 1 every time, which is probably normal. As seen at how to run background job in web.py?, it seems I need to instead run the script via SSH, e.g:

python app.py 1234 > log.txt 2>&1 &

to run on port 1234 (as the default of 8080 is "in use"). This appears to work, but I have no idea where to go to see the running app on my website. How do I connect to this port 1234? myurl.com:1234 does not work, and the url of the script still has the same behavior as before. I imagine the answer is not specific to web.py, but is something fundamental I do not understand about how these things work - my Google searches are not returning anything useful, probably because I am not asking the right question.




Aucun commentaire:

Enregistrer un commentaire