title: How does mod_wsgi know and execute the application?
body: <p>I'm tying to setup Apache/2.2.22 (Debian) mod_wsgi/3.3 Python/2.7.3</p>
  
  <p>I manage to get the WSGIScriptAlias executed, but only the top level module code, not the <code>application</code> defined therein.</p>
  
  <p>Apache configuration:</p>
  
  <pre><code>LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
  
  WSGIScriptAlias  /testurl /home/django/test/test/wsgi_test.py
  
  &lt;Directory /home/django/test/test&gt;
  &lt;Files wsgi_test.py&gt;
  Order deny,allow
  Allow from all
  &lt;/Files&gt;
  &lt;/Directory&gt;
  </code></pre>
  
  <p>wsgi_test.py:</p>
  
  <pre><code>#!/usr/bin/python
  import sys
  print &gt;&gt; sys.stderr, "I'm wsgi_test"
  
  def application(environ, start_response):
          print &gt;&gt; sys.stderr, 'in application'
          status = '200 OK'
          output = 'hello World'
          response_headers = [('Content-type', 'text/plain'),
                              ('Content-Length', str(len(output)))]
          start_response(status, response_headers)
          return [output]
  </code></pre>
  
  <p>When requesting the url using a browser the <code>wsgi_test.py</code> script gets executed ("I'm wsgi_test" appearing in the apache error log). However, no page is served (500 internal server error) and there is an additional error log entry <em>Premature end of script headers: wsgi_test.py</em>.</p>
  
  <p>As a second test, I used a simple script, which correctly serves 'Hello World':</p>
  
  <p>wsgi_test2.py:</p>
  
  <pre><code>#!/usr/bin/python
  import sys
  print 'Content-type: text/plain\n\n'
  print 'Hello World'
  </code></pre>
  
  <p>My question:
  <strong>How does mod_wsgi know and execute the application?</strong></p>
  
  <p>From the above tests, I conclude that <code>wsgi_test.py</code> is immediately executed. Since there is no executable code but only a definition of <code>application</code>, the script does not output anything and thus the server complains about the missing html headers. How do I tell the system to run the <code>application</code>?</p>