我正在使用 Flask 构建一个网站,我想使用指示的SubdomainDispatcher,但我不知道如何使它工作(它缺少run
启动服务器的初始值)。
这是代码:
这里给出的 create_app 函数:
def create_app(database_uri, debug=False):
app = Flask(__name__)
app.debug = debug
# set up your database
app.engine = create_engine(database_uri)
# add your modules
app.register_module(frontend)
# other setup tasks
return app
子域调度程序:
class SubdomainDispatcher(object):
def __init__(self, domain, create_app):
self.domain = domain
self.create_app = create_app
self.lock = Lock()
self.instances = {}
def get_application(self, host):
host = host.split(':')[0]
assert host.endswith(self.domain), 'Configuration error'
subdomain = host[:-len(self.domain)].rstrip('.')
with self.lock:
app = self.instances.get(subdomain)
if app is None:
app = self.create_app(subdomain)
self.instances[subdomain] = app
return app
def __call__(self, environ, start_response):
app = self.get_application(environ['HTTP_HOST'])
return app(environ, start_response)
主应用程序:
def make_app(subdomain):
user = get_user_for_subdomain(subdomain)
if user is None:
# if there is no user for that subdomain we still have
# to return a WSGI application that handles that request.
# We can then just return the NotFound() exception as
# application which will render a default 404 page.
# You might also redirect the user to the main page then
return NotFound()
# otherwise create the application for the specific user
return create_app(user)
application = SubdomainDispatcher('example.com', make_app)
到目前为止,我的代码可以正常工作,但随后就停止了。这是正常的,因为没有地方,有:
if __name__ == "__main__":
application = create_app(config.DATABASE_URI, debug=True)
application.run()
使初始服务器运行的代码。
我试过这个:
if __name__ == "__main__":
application = SubdomainDispatcher('example.com', make_app)
application.run()
但它失败了:
AttributeError:“SubdomainDispatcher”对象没有属性“运行”
如何使用 SubdomainDispatcher 运行它?