Display Session variables and their values using Python

Is there a way to display sessions and their contents using Python? So far, I can only display the starting session cookie name and the value of it. That’s about it, but is there a way to display all session variables and their values?

Like with PHP, you just do

print_r($_SESSION);

And you basically get all the session variables and their values, but with little knowledge in Python, I want to learn it. I suspect there is such a thing like PHP’s $_SESSION variable.

I’ve been looking for hours on Google, but no hits. Can someone point me in the right direction?

So far, I have this.

import os

if "HTTP_COOKIE" in os.environ:
    print os.environ["HTTP_COOKIE"]
else:
    print "HTTP_COOKIE not set!"
1 Like

So I am guessing there’s no way of displaying sessions using Python? I’ve looked some more and still nothing.


EDIT


I was thinking that I could be using the wrong syntax so I tried

import sessions
session = sessions.Session()
print session

But it still displays nothing in there. Is there still something wrong with this syntax? All I am trying to do right now is display all sessions that have already been created. So I create new sessions in PHP and I use Python to display those sessions. I want to be able to allow Python to work along side with PHP. I know this sounds really stupid, but since my host supports Python, it wasn’t to hard to just place the Python script in my web folders. Plus, I’m using a Ubuntu partition on my HD so I was able to have Python from the start.

At this time, it’s just going to be a simple “Create a session using PHP and then connect to Python using PHP and then display all the sessions that have been created using Python.” I am not sure if I am going at this the wrong way, but wouldn’t you need to set 2 sessions one in PHP and one in Python so that both PHP and Python know which session to grab? I am saying this because using Python’s os (I am going to slaughter the correct term for this operator because with Python, there’s no tell which is a variable and which is a constant since there’s no way to indicate between variables and constants unless you manually set them yourself.) variable, it displays the starting session cookie. So using the starting session cookie, wouldn’t you be able to display all session variables?

You probably need a web framework like Django to do that.

https://docs.djangoproject.com/en/1.9/topics/http/sessions/

Is that the only way to bring out the sessions? If it is then I am assuming that Python doesn’t have a session_start() and $_SESSION variables per say. I know it’s not like PHP, but I would assume Python has defaults that doesn’t require frameworks.

I’m about 99% certain Python alone does not have a way to manage sessions. Sessions are a web concept and Python alone is not a web language. You need a framework like Django to use it for web development. On the other hand, php is a web language that includes many of the necessary features for doing web development type of work out of box such; providing an api for sessions, server data, http request info, etc. You can’t easily do any of that with Python unless you use Django or some other type of web framework for Python. Its the same thing with java, c, and many other programming languages that were not created explicitly for the web.

2 Likes

Thanks, I’ll look further into Django.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.