LoginSignup
1
2

More than 5 years have passed since last update.

Tornado で「HelloWorld」(HTTP) サーバを作る

Posted at

はじめに

Tornado は Python での Web フレームワーク/ネットワークライブラリです。

インストール

$ pip install tornado --user
Collecting tornado
Collecting singledispatch (from tornado)
  Using cached singledispatch-3.4.0.3-py2.py3-none-any.whl
Collecting certifi (from tornado)
  Using cached certifi-2016.9.26-py2.py3-none-any.whl
Collecting backports-abc>=0.4 (from tornado)
  Using cached backports_abc-0.5-py2.py3-none-any.whl
Requirement already satisfied: six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from singledispatch->tornado)
Installing collected packages: singledispatch, certifi, backports-abc, tornado
Successfully installed backports-abc-0.5 certifi-2016.9.26 singledispatch-3.4.0.3 tornado-4.4.2

「Hello World」(HTTP) サーバ

/ に GET アクセスで「Hello World」を返す Web サーバです。

server.py
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello World\n");

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler)
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

サーバを起動

$ python server.py

クライアントからアクセス

$ wget -q -O - http://localhost:8888/
Hello World

感想

  • おk
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2