Python 为什么我不';不需要在符合WSGI的应用程序中传递所需的2个位置参数吗?
这是我的班级:Python 为什么我不';不需要在符合WSGI的应用程序中传递所需的2个位置参数吗?,python,class,wsgi,Python,Class,Wsgi,这是我的班级: class App(object): def __init__(self, environ, start_response): self.environ = environ self.start_response = start_response self.html = \ b""" <html> <head>
class App(object):
def __init__(self, environ, start_response):
self.environ = environ
self.start_response = start_response
self.html = \
b"""
<html>
<head>
<title>Example App</title>
</head>
<body>
<h1>Example App is working!</h1>
</body>
</html>
"""
def __call__(self):
self.start_response("200 OK", [("Content-type", "text/html"),
('Content-Length', str(len(self.html)))])
return [self.html]
我在Apache错误日志中得到一个类型错误(显然):
TypeError: __init__() missing 2 required positional arguments: 'environ' and 'start_response'\r
问题是,我看到的每一个例子,他们都没有传递这些论点…:
如果每个示例都省略了这些参数,那么如何传递这些参数并避免类型错误呢 您误读了api文档。你的
\uuuuu init\uuuuuu
方法可以获取你想要的任何参数(在你的应用程序中,除了self,你可能不想要任何其他参数)。然后,您的\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
像这样的东西正是你想要的
class App(object):
def __init__(self, name):
self.name = name
self.html = \
b"""
<html>
<head>
<title>{name}</title>
</head>
<body>
<h1>{name} is working!</h1>
</body>
</html>
""".format(name=self.name)
def __call__(self, environ, start_response):
start_response("200 OK", [("Content-type", "text/html"),
('Content-Length', str(len(self.html)))])
return [self.html]
app = App('Example App')
类应用程序(对象):
定义初始化(self,name):
self.name=名称
self.html=\
b.“”
{name}
{name}正在工作!
“”格式(name=self.name)
定义呼叫(自我、环境、启动响应):
开始响应(“200确定”,“内容类型”,“文本/html”),
('Content-Length',str(len(self.html)))]
return[self.html]
app=app('示例app')
您添加了environ并启动了对init的响应-为什么?查看链接页面上的下一个示例,了解编写init的示例。
class Hello(object):
def __call__(self, environ, start_response):
start_response('200 OK', [('Content-type','text/plain')])
return ['Hello World!']
hello = Hello() # ?????????????
class App(object):
def __init__(self, name):
self.name = name
self.html = \
b"""
<html>
<head>
<title>{name}</title>
</head>
<body>
<h1>{name} is working!</h1>
</body>
</html>
""".format(name=self.name)
def __call__(self, environ, start_response):
start_response("200 OK", [("Content-type", "text/html"),
('Content-Length', str(len(self.html)))])
return [self.html]
app = App('Example App')