[FIXED] Program gets stuck with tornado

Issue

I'm trying to make a Raspberry Pi send plain text to my phone over my local network, from where I plan to pick it up.

I tried the following "hello world"-like program from their official website, but I cannot get it to proceed after a point.

import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Ugh, the world? Well.. hello, I guess")


application = tornado.web.Application([
    (r"/", MainHandler),
])

application.listen(8881)

tornado.ioloop.IOLoop.instance().start()

# I cannot get this line to execute!!
print("Hi!!")

Experience: basics of Python, intermediate with Arduino C++, none in networking/web


Solution

You're trying to print to STDOUT after starting the event loop, so that print statement never sees the light of the day. Basically, you're creating a HTTP server at port 8881 that is constantly listening for requests. Whatever logic you wish the server to do needs to be in a callback, like MainHandler

import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Ugh, the world? Well.. hello, I guess")
        # All your SMS logic potentially goes here
        self.write("Sent SMS to xyz...")


application = tornado.web.Application(
    [
        (r"/", MainHandler),
    ]
)

application.listen(8881)

tornado.ioloop.IOLoop.instance().start()

Then trigger the endpoint by making an HTTP call

curl <IP ADDRESS OF YOUR PI>:8881


Answered By - prithajnath
Answer Checked By - Mildred Charles (Python.Easybugfix Admin)

Related Posts

Post a Comment