kuhboeejoe
kuhboeejoe

Reputation: 350

Not able to get web.py sessions to work using python 2.6 on OS X

I can not seem to get this script to work:

import web
web.config.debug=False

urls = (
  '/', 'hello',
  '/bye/', 'bye')


app = web.application(urls, globals())
session = web.session.Session(app, web.session.DiskStore('sessions'),
                              initializer={'count': 0})

class hello:
    def GET(self):
        session.count += 1
        return "You visited " + str(session.count) + " pages."

class bye:
    def GET(self):
        session.kill()
        return ("Bye, web!")

if __name__ == "__main__":
    app.run()

This is code available from the web.py documentation page: http://webpy.org/cookbook/sessions

When I try to access the 'hello' page by going to http://localhost:1234/, the application returns an internal server error.

Here is the output from the terminal upon accessing this resource:

http://0.0.0.0:1234/
Traceback (most recent call last):
  File "/Library/Python/2.6/site-packages/web/application.py", line 237, in process
    return self.handle()
  File "/Library/Python/2.6/site-packages/web/application.py", line 228, in handle
    return self._delegate(fn, self.fvars, args)
  File "/Library/Python/2.6/site-packages/web/application.py", line 409, in _delegate
    return handle_class(cls)
  File "/Library/Python/2.6/site-packages/web/application.py", line 385, in handle_class
    return tocall(*args)
  File "testing.py", line 15, in GET
    session.count += 1
  File "/Library/Python/2.6/site-packages/web/session.py", line 69, in __getattr__
    return getattr(self._data, name)
AttributeError: 'ThreadedDict' object has no attribute 'count'

127.0.0.1:49207 - - [20/Mar/2012 20:34:01] "HTTP/1.1 GET /" - 500 Internal Server Error

Can any web.py expert out there tell me what is going on?

Upvotes: 2

Views: 1198

Answers (5)

Juan Antonio
Juan Antonio

Reputation: 2614

The problem is the python version. I had the same problem and I solved it when I executed 2.7 version of python. Just doing > python2.7 code.py and the sessions works perfectly.

Upvotes: 0

Lee Li
Lee Li

Reputation: 1

I encounter this problem before. For 500 internal error, please use chmod -R 755 your_foler/sessions
to change your folder's permission.

Upvotes: 0

kuhboeejoe
kuhboeejoe

Reputation: 350

I installed and configured Macports version of Python 2.7.2 and still I get similar results.

It just occurred to me that this may be a permissions issues, because of where the sessions are stored. I tried running with 'sudo' using both Python 2.7 and Python 2.6 that came natively from Apple, and running

sudo python myfile.py 1234

Web.py sessions is now working. @Eduardo Ivanec: I will attempt to set up my Python development environment using your instructions. Thank you again!

Upvotes: 1

Eduardo Ivanec
Eduardo Ivanec

Reputation: 11852

Your code works as is with Python 2.7.2 and web.py 0.36. If you're running anything older I'd upgrade, as the example is valid.

Your initializer doesn't seem to be working correctly. You can try using this snippet instead to initialize the count attribute on its first use:

class hello:
    def GET(self):
        try:
            session.count += 1
        except AttributeError:
            session.count = 1
        return "You visited " + str(session.count) + " pages."

Edit: since you're using OS X, this link may be of help. It describes how to set up a brand new Python environment using standard OS X tools and virtualenv. I use GNU/Linux, but it looks nice. Also, you can download a ready to go version in the official download page.

Upvotes: 0

nickzam
nickzam

Reputation: 813

Change app = web.application(urls, globals())

to app = web.application(urls, locals())

Upvotes: 1

Related Questions