日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:pythontornado如何啟動和配置?

之前簡單的講過了python tornado的安裝,有的小伙伴已經(jīng)迫不及待的想要做出一番成果了。接下來,小編就帶大家學(xué)習(xí)下如何啟動和配置,向高階段的大神進(jìn)發(fā)吧。


如果小伙伴一直關(guān)注這個系列,那么第一步應(yīng)該對你來說習(xí)以為常。

$ mkdir tornado_todo
$ cd tornado_todo
$ pipenv install --python 3.6
$ pipenv shell
(tornado-someHash) $ pipenv install tornado

創(chuàng)建一個 setup.py 文件來安裝我們的應(yīng)用程序相關(guān)的東西:

(tornado-someHash) $ touch setup.py
# setup.py
from setuptools import setup, find_packages
 
requires = [
    'tornado',
    'tornado-sqlalchemy',
    'psycopg2',
]
 
setup(
    name='tornado_todo',
    version='0.0',
    description='A To-Do List built with Tornado',
    author='',
    author_email='',
    keywords='web tornado',
    packages=find_packages(),
    install_requires=requires,
    entry_points={
        'console_scripts': [
            'serve_app = todo:main',
        ],
    },
)

因?yàn)?Tornado 不需要任何外部配置,所以我們可以直接編寫 Python 代碼來讓程序運(yùn)行。讓我們創(chuàng)建 todo 目錄,并用需要的前幾個文件填充它。

todo/
    __init__.py
    models.py
views.py

 就像 Flask 和 Pyramid 一樣,Tornado 也有一些基本配置,放在 __init__.py 中。從 tornado.web 中,我們將導(dǎo)入 Application 對象,它將處理路由和視圖的連接,包括數(shù)據(jù)庫(當(dāng)我們談到那里時再說)以及運(yùn)行 Tornado 應(yīng)用程序所需的其它額外設(shè)置。

# __init__.py
from tornado.web import Application
 
def main():
    """Construct and serve the tornado application."""
app = Application()

像 Flask 一樣,Tornado 主要是一個 DIY 框架。當(dāng)構(gòu)建我們的 app 時,我們必須設(shè)置該應(yīng)用實(shí)例。因?yàn)?Tornado 用它自己的 HTTP 服務(wù)器來提供該應(yīng)用,我們必須設(shè)置如何提供該應(yīng)用。首先,在 tornado.options.define 中定義要監(jiān)聽的端口。然后我們實(shí)例化 Tornado 的 HTTPServer,將該 Application 對象的實(shí)例作為參數(shù)傳遞給它。

# __init__.py
from tornado.httpserver import HTTPServer
from tornado.options import define, options
from tornado.web import Application
 
define('port', default=8888, help='port to listen on')
 
def main():
    """Construct and serve the tornado application."""
    app = Application()
    http_server = HTTPServer(app)
http_server.listen(options.port)

當(dāng)我們使用 define 函數(shù)時,我們最終會在 options 對象上創(chuàng)建屬性。第一個參數(shù)位置的任何內(nèi)容都將是屬性的名稱,分配給 default 關(guān)鍵字參數(shù)的內(nèi)容將是該屬性的值。

例如,如果我們將屬性命名為 potato 而不是 port,我們可以通過 options.potato 訪問它的值。

在 HTTPServer 上調(diào)用 listen 并不會啟動服務(wù)器。我們必須再做一步,找一個可以監(jiān)聽請求并返回響應(yīng)的工作應(yīng)用程序,我們需要一個輸入輸出循環(huán)。幸運(yùn)的是,Tornado 以 tornado.ioloop.IOLoop 的形式提供了開箱即用的功能。

# __init__.py
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import define, options
from tornado.web import Application
 
define('port', default=8888, help='port to listen on')
 
def main():
    """Construct and serve the tornado application."""
    app = Application()
    http_server = HTTPServer(app)
    http_server.listen(options.port)
    print('Listening on http://localhost:%i' % options.port)
IOLoop.current().start()

我喜歡某種形式的 print 語句,來告訴我什么時候應(yīng)用程序正在提供服務(wù),這是我的習(xí)慣。如果你愿意,可以不使用 print。

我們以IOLoop.current().start() 開始我們的 I/O 循環(huán)。讓我們進(jìn)一步討論輸入,輸出和異步性。

以上就是python tornado啟動和配置的方法。更多Python學(xué)習(xí)推薦:PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。


當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)Python教程:pythontornado如何啟動和配置?
URL分享:http://www.dlmjj.cn/article/djecsch.html