문제

See section "The Application/Framework Side" of PEP-3333.

It asks me to create a module with simple_app function. So I wrote this code in /www/app.py:

def simple_app(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b'<p>Hello World</p>']

Then I added a file for my site in /etc/nginx/sites-enabled.

server {
    listen 8080;
    root /www;
    index index.html index.htm;
    server_name foo;
    location / {
        uwsgi_pass 127.0.0.1:9090;
    }
}

Then I started my uwsgi server in this way:

uwsgi --socket 127.0.0.1:9090 --wsgi-file /www/app.py

Finally, I restarted nginx and visited my site and I get "Internal Server Error".

In the uwsgi log I see this error:

--- no python application found, check your startup logs for errors ---

If I rename simple_app in my module to application, everything works fine.

My question is: If the function must be named application for it to be recognized by a WSGI server, then why does PEP-3333 suggest simple_app? If I have misunderstood PEP-3333, and if it indeed asks us to define application callable, could you please quote the document?

By the way, I know one is supposed to be using a framework to do Python web development, but I am just trying to get a hello world program working with bare WSGI. I feel I should always know how to do a hello world with WSGI alone so that I can appreciate what the frameworks are doing better.

도움이 되었습니까?

해결책

The PEP defines that you should create a callable, and does not stipulate what name it should use. The name simple_app() is just an example name; below it is another example that uses a class named AppClass.

Further on, in the Application Configuration section the PEP states:

This specification does not define how a server selects or obtains an application to invoke. These and other configuration options are highly server-specific matters. It is expected that server/gateway authors will document how to configure the server to execute a particular application object, and with what options (such as threading options).

Each WSGI server needs to be configured to load the callable, but often they have a default. uwsgi looks for an object named application as its default.

You can tell uwsgi to look for a different name by using the callable option:

Argument: string Default: application

Set default WSGI callable name.

For your example that'd be:

uwsgi --socket 127.0.0.1:9090 --wsgi-file /www/app.py --callable simple_app
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top