此基於Web.py的簡單模塊允許您從高級argparse.ArgumentParser對象和類似( argh.ArgumentParser )中自動設置簡單的HTTP Web服務器。在ARGH頂部使用此功能,您可以自動從應用程序中定義的簡單功能生成Web用戶界面。此軟件包是為了將您的個人命令行腳本進入下一階段 - 內部共享實用程序。
對於像設置這樣的製作,您需要:
通過調用webui.webui.wsgi()方法使您的主要腳本公開應用程序全局對象
修改index.wsgi適合您的應用程序(瑣碎的配置,導入上述應用程序)
設置支持Apache(或任何其他)服務器的WSGI
對於像設置這樣的調試,您需要(但由於它用於內部工具,這也可能很好):
argparse.ArgumentParser.parse_args()或argh.dispatch()的方法,分別為webui.Webui.getone()或webui.Webui.dispatch() 。 dispatch()將實例化Web服務並調用調度方法(用戶提供 - 您 - 或支持參數解析器(如argh )的調度方法)
get()和getone()包含dispatch()方法和收益結果,並在Web表單中提交結果,提供了一個類似於parse_args()方法的接口。
argparseweb需要web.py可用。您可以使用: pip install web.py安裝它(查看最新版本)
此示例將設置HTTP服務器,獲取一個有效的輸入,向下撕下HTTP服務器,打印一條歡迎消息以示出:
import argparse
from argparseweb import *
def main ():
parser = argparse . ArgumentParser ()
parser . add_argument ( "name" , default = "Anonymous" )
# previously opts = parser.parse_args()
opts = webui . Webui ( parser ). getone ()
print ( "Hello {name}, n this is a simple example." . format ( name = opts . name ))
if __name__ == "__main__" :
main ()此示例還將運行直到停止,為每個有效輸入打印一條歡迎消息:
import argparse
from argparseweb import *
def main ():
parser = argparse . ArgumentParser ()
parser . add_argument ( "name" , default = "Anonymous" )
# previously opts = parser.parse_args()
for opts in webui . Webui ( parser ). get ():
print ( "Hello {name}, n this is a simple example." . format ( name = opts . name ))
if __name__ == "__main__" :
main ()此示例將在HTTP響應中打印歡迎消息,將其發送回用戶:
import argparse
from argparseweb import *
def welcome ( opts ):
print ( "Hello {name}, n this is a simple example." . format ( name = opts . name ))
def main ():
parser = argparse . ArgumentParser ()
parser . add_argument ( "name" , default = "Anonymous" )
# previously opts = parser.parse_args()
webui . Webui ( parser ). dispatch ( welcome , parsed = True )
if __name__ == "__main__" :
main ()該片段包括WebUI實用程序的三種操作模式:
首先,最簡單:使用ARGH的自動函數來命令行解析器設施的調度方法,這與WebUI完全無關,因此,您將不會失去現有的命令行使用能力。
獲取--webui作為第一個命令行參數,設置開發Web服務器(默認為 *:8080),並可以使用。
公開支持WSGI接口的application全局對象。一旦您用正確的WSGI配置指向瀏覽器(對我來說是有點痛苦),它將像魔術一樣工作:)
myapp.py:
import argparse
from argparseweb import *
def get_parser ():
"""Generate generic argument parser"""
cmd_parser = argh . ArghParser ()
cmd_parser . add_commands ([...])
return cmd_parser
def main_1 ():
# k. get the parser as usual
cmd_parser = get_parser ()
# last chance to use webui, if --webui is passed as first command line argument
# remove it as let webui handle the rest
if sys . argv [ 1 ] == '--webui' :
sys . argv . remove ( '--webui' )
webui . Webui ( cmd_parser ). dispatch () # second mode of operation - development/fast setup
else :
# dispatch either webui or argh
cmd_parser . dispatch () # first mode of operation - regular command line
def main_2 ():
parser = argparse . ArgumentParser ()
# TODO: fill argparse
# opts = parser.parse_args()
opts = webui . Webui ( parser ). getone ()
# TODO: use opts as you would with any ArgumentParser generated namespace,
# opts is really a namespace object directly created by parser, and webui only compiled an argument sequence
# based on the filled form, passed into parser.parse_args() and back to you
def wsgi ():
global application
# create a webui application using the command line argument parser object
# and make it's wsgi function the global `application` parameter as required by wsgi
cmd_parser = get_parser ()
application = webui . Webui ( cmd_parser ). wsgi () # third mode of operation - production wsgi application
if __name__ == "__main__" :
# script initialized as main, lets do our trick
main ()
else :
# if script wasn't initialized as main script, we're probably running
# in wsgi mode
wsgi ()index.wsgi:
# TODO: replace with your application path
# i found now way to get it automatically in wsgi :/
APP_DIR = '/var/www/myapp'
import sys , os
sys . path . insert ( 0 , APP_DIR )
os . chdir ( APP_DIR )
from myapp import application更多示例正在test.py中
完畢: