2016-11-18 6 views
1

私はapp.pyという名前のシンプルなTurboGearsの2スクリプトを、持っている: "!こんにちは、世界を" 私はapp.pyを実行し、http://localhost:5000/all__thingsを訪れたとき、私は見TurboGearsはURLのどの文字を置き換えますか?

#!/usr/bin/env python3 

from wsgiref.simple_server import make_server 
from tg import expose, TGController, AppConfig 

class RootController(TGController): 
    @expose() 
    def all__things(self): 
     return "Hello world!" 

config = AppConfig(minimal=True, root_controller=RootController()) 

print("Serving on port 5000...") 
httpd = make_server('', 5000, config.make_wsgi_app()) 
httpd.serve_forever() 

を予想通り。しかし、これらのURLも動作:

http://localhost:5000/all--things 
http://localhost:5000/[email protected]@things 
http://localhost:5000/all$$things 
http://localhost:5000/all++things 
http://localhost:5000/all..things 
http://localhost:5000/all,,things 

ならびにそれらの組み合わせ:

http://localhost:5000/all-_things 
http://localhost:5000/all_-things 
http://localhost:5000/[email protected] 
http://localhost:5000/[email protected] 
http://localhost:5000/[email protected] 
http://localhost:5000/[email protected]$things 

エトセトラ... TurboGearsのにアンダースコアに置換することができる文字の完全なリストは何

URL?

また、この機能を特定の文字に置き換えるように制限できますか?理想的には、ダッシュ(http://localhost:5000/all--things)付きのURLを動作させ、アンダースコア(http://localhost:5000/all__things)または他の奇妙な文字を含むURLは機能しません。

答えて

1

path_translatorによって管理されており、のdispatch_path_translatorオプションで設定できます。 Noneを渡すか、カスタム関数を提供することで無効にすることができます。

提供されるすべての機能は、現在処理中のパスの一部を受け取り、それを正規化して返す必要があります。

翻訳者がstring.punctuationに基づいてデフォルトのパスは、(https://github.com/python/cpython/blob/c30098c8c6014f3340a369a31df9c74bdbacc269/Lib/string.py#L31を参照)

あなたは@routeデコレータを通じて、より複雑なケースであなたを助けるかもしれない、私はあなたがhttps://github.com/TurboGears/tgext.routes検討し提案するカスタムルーティングニーズを持っている場合。

+0

'config.dispatch_path_translator = False'を設定するとプログラムがクラッシュしますが、' config.dispatch_path_translator = None'は動作します。最後に私は決めました: 'config.dispatch_path_translator = lambda path_piece:path_piece.replace( ' - '、 '_')path_piece以外の場合 '' ' ありがとうございました。 –

+0

ああ、申し訳ありません、それは真/なし/機能です 私は真の反対として反射で偽を書いた:D 答えを更新しました – amol

関連する問題