index.wsgi > index.py

138 views
Skip to first unread message

nitin chandra

unread,
Oct 20, 2010, 11:56:23 AM10/20/10
to mod...@googlegroups.com
Hello All,

I have set up mod_wsgi + apache on Ubuntu with SSL enabled. And it works.

I am able to see Hello World.

Now i have also keyed in index.py as follows and done a soft link to
index.wsgi. i really want to work in this way and p, just bear with me
till i get it to run for few months and onwards.

Thank you

Nitin
-------------------

In Apache 's httpd.conf I have also set 'DirectoryIndex index.html
index.wsgi index.py'

On entering https://healthcare.in/index.py

***
500 Internal Server Error

The server encountered an internal error or misconfiguration and was
unable to complete your request.

Please contact the server administrator, y...@healthcare.in and inform
them of the time the error occurred, and anything you might have done
that may have caused the error.

More information about this error may be available in the server error log.

****


index.py
----------------
import os
from datetime import datetime
import re

class Handles:
def do(self, environ, start_response):

uri = environ['REQUEST_URI']

html = "<html><head><title>Index of %s</title></head><body>" % uri
html += "<h1>Index of %s</h1>" % uri
html += "<table border='0'>"
html += '<tr><th>Last
Modified</th><th>Size</th>Description</th><th>Webifiable</th><tr><th
colspan="6"><hr></th></tr>
html += '<tr><td><a href="..">Parent
Directory</a></td><td>&nbsp;</td><td align="right"> -
</td><td>&nbsp;</td><td>&n
html += '<tr><th colspan="6"><hr></th></tr>'
html += "</tables>"

html += "</body></html>"

output = html
mimeType = 'text/html'

status = '200 OK'
response_headers = [('Content-type', mimeType),
('content-Length' , str(len(output)))]

start_response(status, start_response):
return [output]

def application(environ, start_response):
handler = Handler()
return handler.do(environ, start_response)

Graham Dumpleton

unread,
Oct 20, 2010, 10:49:08 PM10/20/10
to mod...@googlegroups.com
And when you went to the Apache error log as the message suggested you
should, what did you find?

Graham

> --
> You received this message because you are subscribed to the Google Groups "modwsgi" group.
> To post to this group, send email to mod...@googlegroups.com.
> To unsubscribe from this group, send email to modwsgi+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/modwsgi?hl=en.
>
>

nitin chandra

unread,
Oct 21, 2010, 1:16:50 AM10/21/10
to mod...@googlegroups.com
The following is from my apache/log/error_log

[Wed Oct 20 21:22:57 2010] [error] [client 192.168.1.9] Target WSGI
script not found or unable to stat: /home/dev/wsgi-scrip
[Wed Oct 20 21:26:25 2010] [error] [client 192.168.1.9] Target WSGI
script not found or unable to stat: /home/dev/wsgi-scrip
[Thu Oct 21 10:41:28 2010] [warn] RSA server certificate CommonName
(CN) `ubuntu' does NOT match server name!?
[Thu Oct 21 10:41:28 2010] [notice] suEXEC mechanism enabled (wrapper:
/opt/apache2215/bin/suexec)
[Thu Oct 21 10:41:28 2010] [notice] Digest: generating secret for
digest authentication ...
[Thu Oct 21 10:41:28 2010] [notice] Digest: done
[Thu Oct 21 10:41:29 2010] [warn] RSA server certificate CommonName
(CN) `ubuntu' does NOT match server name!?
[Thu Oct 21 10:41:29 2010] [warn] pid file
/opt/apache2215/logs/httpd.pid overwritten -- Unclean shutdown of
previous Apache
[Thu Oct 21 10:41:29 2010] [notice] Apache/2.2.15 (Unix)
mod_ssl/2.2.15 OpenSSL/0.9.8k DAV/2 mod_wsgi/3.2 Python/2.6.5 confi
[Thu Oct 21 10:41:45 2010] [error] [client 192.168.1.9] Symbolic link
not allowed or link target not accessible: /home/dev/w
[Thu Oct 21 10:41:45 2010] [error] [client 192.168.1.9] Target WSGI
script not found or unable to stat: /home/dev/wsgi-scrip
[Thu Oct 21 10:41:50 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1329): Target WSGI script '/home/dev/wsgi-scripts/inde
[Thu Oct 21 10:42:36 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:36 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:36 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:36 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:36 2010] [notice] caught SIGTERM, shutting down
[Thu Oct 21 10:42:37 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:37 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:37 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:37 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:37 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth
[Thu Oct 21 10:42:37 2010] [error] Exception KeyError:
KeyError(139750291932960,) in <module 'threading' from '/usr/lib/pyth

Nitin

>>       start_response(status, response_headers)

Graham Dumpleton

unread,
Oct 21, 2010, 1:22:27 AM10/21/10
to mod...@googlegroups.com
On 21 October 2010 16:16, nitin chandra <nitinc...@gmail.com> wrote:
> The following is from my apache/log/error_log
>
> [Wed Oct 20 21:22:57 2010] [error] [client 192.168.1.9] Target WSGI
> script not found or unable to stat: /home/dev/wsgi-scrip
> [Wed Oct 20 21:26:25 2010] [error] [client 192.168.1.9] Target WSGI
> script not found or unable to stat: /home/dev/wsgi-scrip
> [Thu Oct 21 10:41:28 2010] [warn] RSA server certificate CommonName
> (CN) `ubuntu' does NOT match server name!?
> [Thu Oct 21 10:41:28 2010] [notice] suEXEC mechanism enabled (wrapper:
> /opt/apache2215/bin/suexec)
> [Thu Oct 21 10:41:28 2010] [notice] Digest: generating secret for
> digest authentication ...
> [Thu Oct 21 10:41:28 2010] [notice] Digest: done
> [Thu Oct 21 10:41:29 2010] [warn] RSA server certificate CommonName
> (CN) `ubuntu' does NOT match server name!?
> [Thu Oct 21 10:41:29 2010] [warn] pid file
> /opt/apache2215/logs/httpd.pid overwritten -- Unclean shutdown of
> previous Apache
> [Thu Oct 21 10:41:29 2010] [notice] Apache/2.2.15 (Unix)
> mod_ssl/2.2.15 OpenSSL/0.9.8k DAV/2 mod_wsgi/3.2 Python/2.6.5 confi
> [Thu Oct 21 10:41:45 2010] [error] [client 192.168.1.9] Symbolic link
> not allowed or link target not accessible: /home/dev/w

Looks like you haven't enabled FollowSymLinks in Apache or your
symlink is broken.

Graham

nitin chandra

unread,
Oct 21, 2010, 2:13:06 AM10/21/10
to mod...@googlegroups.com
These are teh enteries in my httpd.conf


<Directory "/opt/apache2215/htdocs">
Options Indexes FollowSymLinks
</Directory>

<VirtualHost 192.168.1.6:80>
ServerName www.healthcare.in
ServerAlias healthcare.in
Redirect / https://www.healthcare.in/index.wsgi
.
.
<Directory /home/dev/wsgi-scripts>
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
.
.
</VirtualHost>

<VirtualHost 192.168.1.6:443>
DocumentRoot "/home/dev/wsgi-scripts/"
ServerName www.healthcare.in
.
.
.
<Directory /home/dev/wsgi-scripts>
Options FollowSymLinks
Order allow,deny
Allow from all
SSLOptions +StdEnvVars
Options ExecCGI
SetHandler wsgi-script
WSGIProcessGroup healthcare.in
</Directory>
</VirtualHost>


Reg
Nitin

nitin chandra

unread,
Oct 21, 2010, 5:14:34 AM10/21/10
to mod...@googlegroups.com
some help ... pl ...

nitin chandra

unread,
Oct 21, 2010, 2:53:37 PM10/21/10
to mod...@googlegroups.com
Ok ... i think here is the culprit ... which has been troubling me ...
"application" not found in script.... gives an internal server error
'500'

[Thu Oct 21 23:18:43 2010] [notice] Apache/2.2.15 (Unix)


mod_ssl/2.2.15 OpenSSL/0.9.8k DAV/2 mod_wsgi/3.2 Python/2.6.5

configured -- resuming normal operations
[Thu Oct 21 23:18:47 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1794): Target WSGI script '/home/dev/wsgi-scripts/index.py' does
not contain WSGI application 'application'.
[Thu Oct 21 23:18:47 2010] [error] [client 192.168.1.9] Target WSGI
script not found or unable to stat: /home/dev/wsgi-scripts/favicon.ico
[Thu Oct 21 23:20:13 2010] [error] [client 192.168.1.9] Target WSGI
script not found or unable to stat: /home/dev/wsgi-scripts/favicon.ico
[Thu Oct 21 23:20:19 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1796): Target WSGI script '/home/dev/wsgi-scripts/index.py' does
not contain WSGI application 'application'.
[Thu Oct 21 23:28:20 2010] [error] [client 192.168.1.9] Target WSGI
script not found or unable to stat: /home/dev/wsgi-scripts/favicon.ico
[Thu Oct 21 23:34:15 2010] [error] [client 192.168.1.9] Target WSGI
script not found or unable to stat: /home/dev/wsgi-scripts/favicon.ico
[Thu Oct 21 23:34:16 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1797): Target WSGI script '/home/dev/wsgi-scripts/index.py' does
not contain WSGI application 'application'.
[Thu Oct 21 23:34:19 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1792): Target WSGI script '/home/dev/wsgi-scripts/index.py' does
not contain WSGI application 'application'.
[Thu Oct 21 23:34:28 2010] [error] Exception KeyError:
KeyError(140078841251616,) in <module 'threading' from
'/usr/lib/python2.6/threading.pyc'> ignored
[Thu Oct 21 23:34:28 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1793): Target WSGI script '/home/dev/wsgi-scripts/index.py' does
not contain WSGI application 'application'.
[Thu Oct 21 23:40:19 2010] [error] [client 192.168.1.9] Target WSGI
script not found or unable to stat: /home/dev/wsgi-scripts/favicon.ico
[Thu Oct 21 23:40:20 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1793): Target WSGI script '/home/dev/wsgi-scripts/index.py' does
not contain WSGI application 'application'.
[Thu Oct 21 23:40:21 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1792): Target WSGI script '/home/dev/wsgi-scripts/index.py' does
not contain WSGI application 'application'.
cat: last: No such file or directory

--------------------------------------
#!/usr/bin/env python

import os
from datetime import datetime
import re

class Handler:
def do(self, environ, start_response):

uri = environ['REQUEST_URI']

html = "<html><head><title>Index of %s</title></head><body>" % uri
html += "<h1>Index of %s</h1>" % uri
html += "<table border='0'>"
html += '<tr><th>Last
Modified</th><th>Size</th>Description</th><th>Webifiable</th><tr><th
colspan="6"><hr></th></tr>
html += '<tr><td><a href="..">Parent
Directory</a></td><td>&nbsp;</td><td align="right"> -
</td><td>&nbsp;</td><td>&n
html += '<tr><th colspan="6"><hr></th></tr>'
html += "</tables>"

html += "</body></html>"

output = html
mimeType = "text/html"

status = "200 OK"
response_headers = [("Content-type", mimeType),

("Content-length" , str(len(output)))]

start_response(status, response_headers)
return [output]

def application(environ, start_response):
handler = Handler()
return handler.do(environ, start_response)

### Do I REALLY need to use wsgiref ??? Else how do I run 'application' ???

if __name__ == '__main__' :
## Handler() ;;; will this do
import wsgiref.handlers
wsgiref.handlers.CGIHandler().run(application)


>>
>> <Directory "/opt/apache2215/htdocs">
>>    Options Indexes FollowSymLinks
>> </Directory>

FollowSymLinks changed to MultiViews

TIA

Nitin

Jason Garber

unread,
Oct 21, 2010, 3:49:50 PM10/21/10
to mod...@googlegroups.com
WSGI requires that you have a callable called `application` defined directly in your *.wsgi file...  

In your code, application is just a method of your Handler class, so it's not going to work at all.  Consider using one of these three patterns...

# Pattern 1
def application(environ, start_response):
   start_resposne(...)
   return [content]

# Pattern 2
class MyApplication:
   def __init__(self):
      ...
   def __call__(self, environ, start_response):
      start_response(...)
      return [content]

application = MyApplication()

# Pattern 3
class application:
   def __new__(cls, environ, start_response)
      self = object.__new__(cls)   # is this right?
      content = self.DoSomething(environ)
      start_response(...)
      return [content]


Deron Meranda

unread,
Oct 22, 2010, 12:20:36 AM10/22/10
to mod...@googlegroups.com
On Thu, Oct 21, 2010 at 3:49 PM, Jason Garber <bo...@gahooa.com> wrote:
> ... In your code, application is just a method of your Handler class, so it's
> not going to work at all.  Consider using one of these three patterns...

Or a 4th way to just un-indent the definition of the application()
method so it is instead a top-level function:

def application(environ, start_response):
handler = Handler()
return handler.do(environ, start_response)

Actually, I'm wondering if that wasn't what was intended all along, but
there was just an inadvertent indentation error.

--
Deron Meranda
http://deron.meranda.us/

Graham Dumpleton

unread,
Oct 22, 2010, 12:22:18 AM10/22/10
to mod...@googlegroups.com

Given that there was no 'self' argument, probably likely. :-)

Graham

nitin chandra

unread,
Oct 22, 2010, 3:04:20 PM10/22/10
to mod...@googlegroups.com
SO spent a better half of the day and evening and night ...tried so
many times :(
and same.... along with Indentation error

Thanks
Nitin


500 Internal Server Error

[Fri Oct 22 23:35:04 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=4210, process='healthcare.in',
application='www.healthcare.in|/index.py'): Failed to parse WSGI
script file '/home/dev/wsgi-scripts/index.py'.
[Fri Oct 22 23:35:04 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=4210): Exception occurred processing WSGI script
'/home/dev/wsgi-scripts/index.py'.
[Fri Oct 22 23:35:04 2010] [error] [client 192.168.1.9] File
"/home/dev/wsgi-scripts/index.py", line 31
[Fri Oct 22 23:35:04 2010] [error] [client 192.168.1.9] def
application(environ, start_response):
[Fri Oct 22 23:35:04 2010] [error] [client 192.168.1.9]
^
[Fri Oct 22 23:35:04 2010] [error] [client 192.168.1.9]
IndentationError: unindent does not match any outer indentation level
[Fri Oct 22 23:35:04 2010] [error] [client 192.168.1.9] Target WSGI


script not found or unable to stat: /home/dev/wsgi-scripts/favicon.ico


IS there some editor which does not insert some character without my
knowledge.<frustrated>

------------------------------------------
#!/usr/bin/env python

import os
from datetime import datetime
import re

class Handler:
def do(self, environ, start_response):

uri = environ['REQUEST_URI']

html = "<html><head><title>Index of %s</title></head><body>" % uri
html += "<h1>Index of %s</h1>" % uri
html += "<table border='0'>"
html += '<tr><th>Last
Modified</th><th>Size</th>Description</th><th>Webifiable</th><tr><th
colspan="6"><hr></th></tr>'
html += '<tr><td><a href="..">Parent
Directory</a></td><td>&nbsp;</td><td align="right"> -

</td><td>&nbsp;</td><td>&nbsp;</td></tr>'


html += '<tr><th colspan="6"><hr></th></tr>'
html += "</tables>"

html += "</body></html>"

output = html
mimeType = "text/html"

status = "200 OK"
response_headers = [("Content-type", mimeType),
("Content-length", str(len(output)))]


def application(self, environ, start_response): <line on which all
kind of trials with no luck>
start_response(status, response_headers)
return [output]

nitin chandra

unread,
Oct 24, 2010, 2:56:41 AM10/24/10
to mod...@googlegroups.com
Well tried a fresh approach ... and here are the following setting of
httpd.conf and index.py
I am sure this must be easy ... but i am not able to locate the issue,
and therefore trouble shoot it.... else it must NOT be 'this'
difficult to display the simple html lines.
This is on Ubuntu 64bit (VM) 10.04,
my "index.wsgi" and "index.py" are in '/home/dev/wsgi-scripts/'
directory. LATER, will shift 'index.py' and other *.py and various
project related files, into another directory (
/home/dev/healthcare.in/).

Error is "500 Internal Server Error" and in end, error_log entries are pasted.

< not ready to give up .... yet >

As I keep getting familiar with the development architecture, i shall
trouble the list , lot less :)

TIA

Nitin


my httpd.conf
-----------------------
ServerRoot "/opt/apache2215"

Listen 192.168.1.6:80

LoadModule wsgi_module modules/mod_wsgi.so

User daemon
Group daemon

ServerAdmin y...@example.com

ServerName www.healthcare.in

DocumentRoot "/opt/apache2215/htdocs"

<Directory />
Options MultiViews
AllowOverride None
Order deny,allow
Deny from all
</Directory>

<Directory "/opt/apache2215/htdocs">
Options Indexes FollowSymLinks

AllowOverride All


Order allow,deny
Allow from all
</Directory>

<IfModule dir_module>
DirectoryIndex index.html index.wsgi index.py
</IfModule>

<Directory "/opt/apache2215/cgi-bin">
AllowOverride None
Options MultiViews


Order allow,deny
Allow from all
</Directory>

NameVirtualHost 192.168.1.6:80

WSGIDaemonProcess healthcare.in processes=6 threads=15 display-name=%{GROUP}
WSGIProcessGroup healthcare.in
WSGIScriptAlias / /home/dev/wsgi-scripts/
<Directory /home/dev/wsgi-scripts>
Options MultiViews


Order allow,deny
Allow from all
</Directory>

</VirtualHost>

Listen 192.168.1.6:443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:/tmp/ssl_scache(512000)"
SSLSessionCacheTimeout 300
SSLMutex "file:/tmp/ssl_mutex"

<VirtualHost 192.168.1.6:443>
DocumentRoot "/home/dev/wsgi-scripts/"
ServerName www.healthcare.in

ServerAdmin y...@healthcare.in
SSLEngine on
SSLCipherSuite
ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile "/etc/cups/ssl/server.crt"
SSLCertificateKeyFile "/etc/cups/ssl/server.key"
<FilesMatch "\.(cgi|shtml|phtml|php|py|pyc)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /home/dev/wsgi-scripts>
Options MultiViews


Order allow,deny
Allow from all
SSLOptions +StdEnvVars
Options ExecCGI
SetHandler wsgi-script
WSGIProcessGroup healthcare.in
</Directory>

BrowserMatch ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
</VirtualHost>

<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

==================================================
index.py
----------

#!/usr/bin/env python

import os, re
from datetime import datetime

class Handler:
def do(self, environ, start_response):
uri = environ['REQUEST_URI']
html = "<html><head><title>Index of %s</title></head><body>" % uri
html += "<h1>Index of %s</h1>" % uri
html += "<table border='0'>"
html += '<tr><th>Last

ModifiedL</t><th>Size</th><th>Description</th><th>Webifiable</th><tr><th


colspan="6"><hr></th></tr>'
html += '<tr><td><a href="..">Parent
Directory</a></td><td>&nbsp</td><td align="right"> -
</td><td>&nbsp;</td><td>&nbsp;</td></tr>'
html += '<tr><th colspan="6"><hr></th></tr>'
html += "</tables>"
html += "</body></html>"
output = html
mimeType = "text/html"

status = "200 OK"
response_headers = [("Content-type", mimeType),
("Content-length", str(len(output)))]

application = Handler()

"""
(environ, status_response):
start_response(status, response_header)
return [output]
"""
---------------------------------------
from /opt/apache2215/log/error_log


[Sun Oct 24 11:50:12 2010] [error] [client 192.168.1.9] mod_wsgi
(pid=1414): Exception occurred processing WSGI script
'/home/dev/wsgi-scripts/index.py'.
[Sun Oct 24 11:50:12 2010] [error] [client 192.168.1.9] Traceback
(most recent call last):
[Sun Oct 24 11:50:12 2010] [error] [client 192.168.1.9] File
"/home/dev/wsgi-scripts/index.py", line 24, in <module>
[Sun Oct 24 11:50:12 2010] [error] [client 192.168.1.9]
application = Handler(application)
[Sun Oct 24 11:50:12 2010] [error] [client 192.168.1.9] NameError:
name 'application' is not defined

Jason Garber

unread,
Oct 24, 2010, 11:08:10 AM10/24/10
to mod...@googlegroups.com

Hello,

Please explain how the do method will be called?

There must be a callable in the wsgi file named "application"... As it stands, you have an instance of a "handler" class that cannot be called.

Consider refactoring as a simle function called "application" just to role out those issues... Theb, once working, expand it.

By the way, wsgi itself is a bit raw, so unless your main goal is to develop another framework, I strongly suggest you use a framework.

Both bottle and appstruct are very lightweight.  I can help you off list with the latter...

-jg

nitin chandra

unread,
Oct 25, 2010, 4:02:55 AM10/25/10
to mod...@googlegroups.com
Thanks Jason for the pointer. Need to get my head cleaned ... so
reading some more on the way Python scripts / application are written.
Specially :

class
def func()
_ _init_ _
_ _name_ _ = '_ _main_ _':
_ _call_ _
return

how they get combined and then add them into my code for testing.will
take 2 more days and shall update / request for support on list.

Reg

Nitin

Reply all
Reply to author
Forward
0 new messages