Tornado Error "Hello World"

I am trying to run the following HelloWorld Script on the command line

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options

define("port", default=8888, help="run on the given port", type=int)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")


def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    main()

and I get the following error:

File "helloworld.py", line 17, in ?
    import tornado.httpserver
  File "/home/username/public_html/tornado-1.2.1/tornado/httpserver.py", line 28, in ?
    from tornado import ioloop
  File "/home/username/public_html/tornado-1.2.1/tornado/ioloop.py", line 184
    action if action is not None else signal.SIG_DFL)
            ^
SyntaxError: invalid syntax

Brand new Python, can anyone explain what the problem is? The helloworld.py postscript is in a directory /home/username/public_html/tornado-1.2.1/, and there is a subdirectory in this directory tornado.

Edit: (ignore this edit now) The command I run is -

python helloworld.py

The result python -Vis

Python 2.4.3

Unfortunately, Tornado does not work with versions prior to 2.5, so this can be a problem. However, I installed Python 2.6.6. How can I make sure that it works with the correct version of Python and not older?

EDIT II

Now I installed Python on 2.6.6

and working

python helloworld.py

produces no output. The program just hangs on the command line.

?

+5
2

, , python 2.4 .

Python, . debian Ubuntu /usr/share/python/debian_defaults. Linux- /usr/bin/python :

sudo mv /usr/bin/python /usr/bin/python.dist
sudo ln -s /usr/bin/python2.5 /usr/bin/python

PATH, python /usr/bin (, Windows). , ~/.profile ( ) ~/.bashrc ( , bash ).

+2

Python 2.6 , , python /usr/bin/python2.6 .bash_rc.

, :

if seconds is not None:
        signal.signal(signal.SIGALRM,
                      action if action is not None else signal.SIG_DFL)

(action if action is not None else signal.SIG_DFL) Python 2. >= 5

? , :

if seconds is not None:
        if action is not None:
            tmpaction = action
        else
            tmpaction = signal.SIG_DFL
        signal.signal(signal.SIGALRM,tmpaction)

, . , . (, , ).

+1

All Articles