自学python编程笔记之:11行代码运行一个web服务

平台环境:win7 64位 Python 3.6.1

若没有安装tornado请用以下命令安装:
C:\>pip install tornado

直接上代码:

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tornado import web, ioloop, httpserver
class MainPageHandler(web.RequestHandler):
    def get(self, *args, **kwargs):
        self.write('hello www.itkylin.com')
application = web.Application([(r"/",MainPageHandler),])
if __name__ == '__main__':
    http_server = httpserver.HTTPServer(application)
    http_server.listen(8080)
    ioloop.IOLoop.current().start()

运行代码后在本机打开http://127.0.0.1:8080即可!

上一篇: