Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start a session in a Python web application?

Tags:

python

session

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.

like image 759
Léo Léopold Hertz 준영 Avatar asked Jul 26 '09 20:07

Léo Léopold Hertz 준영


People also ask

How do I start a session in Python?

Use the python command without specifying any parameters to start an interactive Python session. The Python session operates interactively using REPL (Read Evaluate Print Loop) to allow you to write Python code on the command line.

How a session is created in a web application?

A session is defined as a series of related browser requests that come from the same client during a certain time period. Session tracking ties together a series of browser requests—think of these requests as pages—that may have some meaning as a whole, such as a shopping cart application.

How do you use Session Management in Python?

Session Management Varieties Generally, you can choose to manage your Python app's session data in one of two ways: Cookie-based sessions: In this scenario, the session data is not stored in a data store on the back-end. Instead, it's serialized, signed (with a SECRET_KEY), and sent to the client.

How do you define a session in Python?

The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server. The data in the Session is stored on the top of cookies and signed by the server cryptographically.


3 Answers

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.

like image 140
Sean Woods Avatar answered Sep 22 '22 23:09

Sean Woods


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

like image 28
Karim Avatar answered Sep 22 '22 23:09

Karim


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.

like image 35
ars Avatar answered Sep 18 '22 23:09

ars