Reputation: 7855
Here's my class:
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]
And then I run it:
app = App()
I get a Type Error (obviously) in the Apache error log:
TypeError: __init__() missing 2 required positional arguments: 'environ' and 'start_response'\r
The problem is that every example I saw, they just don't pass those arguments... This one for example:
class Hello(object):
def __call__(self, environ, start_response):
start_response('200 OK', [('Content-type','text/plain')])
return ['Hello World!']
hello = Hello() # ?????????????
How am I supposed to pass those arguments and avoid the Type Error, if every example omit them?
Upvotes: 1
Views: 8697
Reputation: 6631
You have misread the api doc. Your __init__
method can take any parameters you want (in your App
example you probably don't want anything other than self). Then your __call__
method is the one that needs to have the environ and start_response parameters, and you don't call __call__
directly, the WSGI server does.
Something like this is what you want..
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')
Upvotes: 3