tags:

views:

511

answers:

4

This question is based on this answer.

I'm looking for a function similar to PHP's session_start() for Python. I want to access a dictionary like $_SESSION in PHP which becomes available after running the command.

+2  A: 

Python is not a web language in itself like PHP, so it has not built in web features. There are many modules that add this functionality however, but then you'll have to be specific about which one you're using.

Here's how you could use it with the Django framework, for example:

def post_comment(request, new_comment):
    if request.session.get('has_commented', False):
        return HttpResponse("You've already commented.")
    c = comments.Comment(comment=new_comment)
    c.save()
    request.session['has_commented'] = True
    return HttpResponse('Thanks for your comment!')

In simpler web frameworks there might not be session support. Sessions aren't impossible to implement yourself, but you can probably find a stand-alone module that adds support by receiving/sending the session id to it (the session id is stored in a cookie, which almost all web frameworks have some kind of support for.)

Blixt
+4  A: 

As someone who comes from PHP and is working his way into Python I can tell you that Django is a good way to start dealing with Python on the web. This is especially true if you've been using MVC frameworks in PHP. That said, Django has built in support for session management and is documented here:

http://docs.djangoproject.com/en/dev/topics/http/sessions/

And, out of curiousity, I took a look around for session management with plain python and found this:

http://code.activestate.com/recipes/325484/

Judging by the comments, it would seem that you're better off using one of the tried and true frameworks to handle this for you. If you're not interested in Django you can also checkout some of the others

Karim
+2  A: 

Let me address some things that might be related to your question...it may not be relevant for you, but I think others might come here with the exact same question and might benefit from my (limited) experience...because I also had this question at one time.

Speaking as someone who went from PHP to Python (and never looked back), I think it's useful to understand how sessions work under the hood. It's probably not a good idea to implement your own session framework unless you (a) want to understand more about sessions management by doing or (b) need something that existing frameworks don't offer.

Wikipedia is always a good place to start. Bottom line: session data gets stored somewhere on the server and indexed by a unique identifier (hash of some sort). This identifier gets passed back and forth between the client and server, usually as a cookie or as part of the query string (the URL). For security's sake, you'll want to use an SSL connection or validate the session ID with some other piece of data (e.g. IP address). By default PHP stores sessions as files, but on a shared server that could pose a security risk, so you might want to override the session engine so you store sessions in a database. Python web frameworks have similar functionality.

When I started doing web programming in Python, I noticed two things. First, PHP wrapped a lot of magic into the language, making it easy for a beginning programmer (me in 2003) to learn the language, but not teaching me much about how everything worked. Therefore, I found myself researching many topics about web applications, specifically database connection pooling, URL mapping, sessions, and threading. PHP (and Django, from what I understand) abstract that away for you. Second, PHP is a really crappy language ;) but it gets the job done!!

Personally I use CherryPy for web development. It has session management as a "tool" that you can turn on.

Sean Woods
@Sean: I am making my first database project at http://stackoverflow.com/questions/1168701/to-make-a-plan-for-my-first-mysql-project You can see that I have not asked much about Python, since I know little about its practical implementation, for instance to CGI (I am not sure whether I need it or not). --- I would appreciate you help, if you could add any concrete tips to your answer, for instance codes or links to easy projects, which would help me in building a terrible simple discussion site.
Masi
@Masi: In the "real world," this project would be too big for the time frame you specify. If I were working under your constraints, I'd use Django. You can read about deployment (which are questions I think you were asking about) at http://docs.djangoproject.com/en/dev/howto/deployment/#howto-deployment-indexI would steer you to mod_wsgi with Apache.
Sean Woods
Oh yeah, one other thing...good luck!
Sean Woods
+1 It's always better to know what the heck is going on so when the magic fails you, you have a fighting chance at resolving the issue.
Tom Willis
+3  A: 

You might consider looking into the Beaker library for Python which isn't tied to any one web framework an will work in a WSGI compatible environment:

http://beaker.groovie.org/

Beaker is a library for caching and sessions for use with web applications and stand-alone Python scripts and applications. It comes with WSGI middleware for easy drop-in use with WSGI based web applications, and caching decorators for ease of use with any Python based application.

ars