问题
Is there any way to pass arguments to web.py handler class constructors?
E.g. These arguments might come from the command line (when the main web.py script is run), after the first arg (which is taken as the port number)
回答1:
Sure, depending on exactly what you mean. It's all python after all.
Consider you have available web.config
which is a Storage
object which is visible everywhere. I regularly use that to place system-wide configuration information (& alter debugging output as you'll see in the example).
Being a Storage object, you can add your own configuration items there & then access them in your url handlers.
if __name__ == '__main__':
app = web.application(urls, globals())
web.config.debug_sql = '--debug_sql' in sys.argv
app.run()
来源:https://stackoverflow.com/questions/10683968/arguments-to-web-py-handler-classes