
- Python Pyramid 教程
- Python Pyramid - 首頁
- Python Pyramid - 概述
- Pyramid - 環境設定
- Python Pyramid - Hello World
- Pyramid - 應用配置
- Python Pyramid - URL 路由
- Python Pyramid - 檢視配置
- Python Pyramid - 路由字首
- Python Pyramid - 模板
- Pyramid - HTML 表單模板
- Python Pyramid - 靜態資源
- Python Pyramid - 請求物件
- Python Pyramid - 響應物件
- Python Pyramid - 會話
- Python Pyramid - 事件
- Python Pyramid - 訊息閃現
- Pyramid - 使用 SQLAlchemy
- Python Pyramid - Cookiecutter
- Python Pyramid - 建立專案
- Python Pyramid - 專案結構
- Python Pyramid - 包結構
- 手動建立專案
- 命令列Pyramid
- Python Pyramid - 測試
- Python Pyramid - 日誌
- Python Pyramid - 安全性
- Python Pyramid - 部署
- Python Pyramid 有用資源
- Python Pyramid - 快速指南
- Python Pyramid - 有用資源
- Python Pyramid - 討論
Python Pyramid - 命令列Pyramid
Pyramid 庫有一個scripts子包,其中包含許多可用於控制和檢查 Pyramid 應用的 Python 指令碼。這些模組既可以作為可匯入的模組使用,也可以從命令提示符使用。因此,它們通常被稱為命令列指令碼。
這些命令列指令碼包括:
pserve - 用於服務使用 PasteDeploy 配置檔案的 Web 應用程式。
pviews - 顯示給定 URL 的匹配檢視。
pshell - 互動式 Shell。
proutes - 顯示所有應用程式路由。
ptweens - 顯示“Tweens”。
prequest - 呼叫請求。
pdistreport - 顯示所有已安裝的釋出版本及其版本。
所有這些命令列指令碼都使用 PasteDeploy 配置檔案 (development.ini)。
pserve
這是最重要的指令碼。在“development.ini”[app:main] 部分配置的 Pyramid 應用程式在所選伺服器 (Waitress) 和指定的宿主和埠 (localhost:6543) 的幫助下提供服務。
假設 Pyramid 專案 (testproj) 是在 Pyramid 虛擬環境中同名資料夾中建立的,則以下命令開始監聽傳入的瀏覽器請求:
Env>..\scripts\pserve development.ini
pserve 模組(以及其他 Pyramid 命令列指令碼)可以在命令提示符下作為 Python 直譯器的引數執行。
Env>python -m pyramid.scripts.pserve development.ini Starting server in PID 1716. 2022-06-23 14:13:51,492 INFO [waitress:485][MainThread] Serving on http://[::1]:6543 2022-06-23 14:13:51,492 INFO [waitress:485][MainThread] Serving on http://127.0.0.1:6543
為了使pserve 實用程式更靈活,可以使用以下命令列引數:
config_uri - 配置檔案的 URI。
-n <name> - 載入指定的應用程式 (預設為 main)。
-s <server_type> - 使用指定的伺服器。
--server-name <section_name> - 使用在配置檔案中定義的指定伺服器 (預設為:main)
--reload - 使用自動重啟檔案監視器。
-b - 在 Web 瀏覽器中開啟伺服器 URL。
應用程式在https://:6543上提供服務,在這種情況下,訪問受到限制,只有在同一臺機器上執行的瀏覽器才能訪問。如果要允許同一網路上的其他機器訪問,則編輯“development.ini”檔案,並將[server:main]部分中的監聽值替換為如下所示:
[server:main] use = egg:waitress#main listen = *:6543
設定 *:6543 等效於 0.0.0.0:6543 [::]:6543,因此,應用程式響應系統擁有的所有 IP 地址上的請求,而不僅僅是針對 localhost 的請求。
pserve 命令列中的 --reload 選項會導致在修改正在執行的程式碼時自動重新載入應用程式。
使用--reload 選項啟動應用程式。
pserve development.ini --reload Starting monitor for PID 36224. Starting server in PID 36224. Serving on https://:6543 Serving on https://:6543
如果對專案的 .py 檔案或 .ini 檔案進行了任何更改,伺服器將自動重啟:
testproj/development.ini changed; reloading ... Gracefully killing the server. Starting monitor for PID 36286. Starting server in PID 36286. Serving on https://:6543 Serving on https://:6543
pviews
pviews 命令列指令碼用於在命令終端視窗中列印給定 URL 的匹配路由和檢視的摘要。pviews 命令接受兩個引數。第一個引數是應用程式“.ini”檔案的路徑及其中的節名稱。這應該是config_file#section_name格式(預設值為 main)。第二個引數是要測試匹配檢視的 URL。
讓我們使用之前用 Cookiecutter 構建的 testproj 專案中的 development.ini 檔案執行 pviews 命令。
Env>..\scripts\pviews development.ini / URL = / context: <pyramid.traversal.DefaultRootFactory object at 0x000001DD39BF1DE0> view name: Route: ------ route name: home route pattern: / route path: / subpath: View: ----- testproj.views.default.my_view
輸出顯示頂部的請求 URL,並在其下方顯示所有匹配的檢視及其檢視配置詳細資訊。在此示例中,只有一個檢視匹配,因此只有一個 View 部分。
pshell
pshell 指令碼可以從 Python 提示符與 Pyramid 應用程式的環境進行互動。此 shell 使用 PasteDeploy 配置檔案,即 development.ini 作為命令列引數(與其他 Pyramid 指令碼一樣),並開啟 Python 互動式 shell。
Env>..\scripts\pshell development.ini Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32 Type "help" for more information. Environment: app The WSGI application. dbsession <sqlalchemy.orm.session.Session object at 0x0000020E9F1452D0> models <module 'testproj.models' from 'f:\\pyram-env\\testproj\\testproj\\models\\__init__.py'> registry Active Pyramid registry. request Active request object. root Root of the default resource tree. root_factory Default root factory used to create `root`. tm Single-thread implementation of `~transaction.interfaces.ITransactionManager`. >>>
指令碼讀取配置,並將其中宣告的物件作為 Python 物件提供,以便與其互動。我們可以從 Python 提示符檢查它們的行為。
>>> root <pyramid.traversal.DefaultRootFactory object at 0x0000020E9E2507F0> >>> registry <Registry testproj>
登錄檔設定從“development.ini”讀取到字典中。我們可以使用 for 迴圈遍歷其內容:
>>> for k,v in registry.settings.items(): ... print (k,":",v) ... pyramid.reload_templates : True pyramid.debug_authorization : False pyramid.debug_notfound : False pyramid.debug_routematch : False pyramid.default_locale_name : en pyramid.includes : pyramid_debugtoolbar sqlalchemy.url : sqlite:///…\testproj/testproj.sqlite retry.attempts : 3 tm.manager_hook : <function explicit_manager at 0x000001D9E64E4550>
甚至可以使用 models.py 中宣告的 SQLAlchemy 模型與資料庫進行互動。
當我們第一次完成 cookiecutter 步驟時,應用程式資料庫在開始時初始化。我們在“testproj.sqlite”資料庫中找到一個 models 表,其中包含一條記錄。

我們現在如下從 Python 提示符訪問此表:
>>> m=models.MyModel >>> obj=dbsession.query(m).get(1) >>> obj <testproj.models.mymodel.MyModel object at 0x0000020E9FD96DA0> >>> obj.name 'one'
讓我們在 models 表中新增新行。首先宣告 MyModel 類的物件,並將其新增到 dbsession 中。
>>> tm.begin() >>> obj=models.MyModel(id=2, name='two', value=2) >>> dbsession.add(obj) >>> tm.commit()
Pyramid 使用事務管理器物件 tm,該物件在pyramid_tm包中宣告。為了確認添加了新記錄,請將其檢索回來。
>>> obj=dbsession.query(models.MyModel).get(2) >>> obj.name 'two'
這也可以透過在 SQLite GUI 工具中實際檢視資料庫的 models 表來確認。

prequest
prequest 實用程式允許您測試 URL 模式的響應,而無需實際啟動伺服器。該命令需要配置檔案和 URL 路徑作為命令列引數。例如:
Env>prequest development.ini /
該命令會生成我們之前看到的 Cookiecutter 首頁的原始 HTML 響應。
可以使用一些命令列開關。-d 選項顯示伺服器返回的狀態和標頭。要覆蓋預設的 GET 請求方法,可以使用 -m 選項。
proutes
此命令列 Pyramid 指令碼顯示新增到應用程式登錄檔的所有路由。它只接受一個引數,即配置檔案 (development.ini)
proutes 命令顯示 testproj 包的以下路由配置:
Env>proutes development.ini Name Pattern View ---- ------- ---- __static/ /static/*subpath testproj:static/ home / testproj.views.default.my_view debugtoolbar /_debug_toolbar/*subpath <unknown> __/_debug_toolbar/static/ /_debug_toolbar/static/*subpath pyramid_debugtoolbar:static/