Solved

Linux environment does not recalled stored value in the os environment

Posted on 2004-08-23
3
272 Views
Last Modified: 2010-04-16
I have the code below on a Linux web server. When a user clicked on a web page, the code below got executed. The first time it runs, it gives a time value in second such as 1093274703.0. 10 seconds later, the user clicked the same web page the second time,
and the message "time has exceed 10 seconds" suppose to appear on the web page, but it is not. This happens because the key COOKIE_TIME_STAMP which was created the first time the user click on a web page does not existed when he clicked on the web page a second time.

=================================
def get_cookie():

  found_key = "false"

  cookie = Cookie.SmartCookie()
  cookie.load(os.environ["HTTP_COOKIE"])

  #The second time, the program is run the code below is suppose to be executed, and
  # the key COOKIE_TIME_STAMP suppose to exist and therefore it is executed, but
  # instead the key  COOKIE_TIME_STAMP  does not exist.

  for key, item in cookie.items():
    if key == "COOKIE_TIME_STAMP":
      found_key = "true"
      shell('echo "ok2" >> /usr/apache/html/abc')
      cookie_time_stamp_2 = item.value
      time_difference = time.mktime(time.localtime(time.time())) - float
                                 (cookie_time_stamp_2);
      if (time_difference > 10):
        return "time has exceed 10 seconds"
        break;

  # the first time the program run, the code below is executed.
  # the code below is not suppose to run the second time because
  # on the second time the key COOKIE_TIME_STAMP suppose to exist
  # and therefore found_key is equal to true.

  if found_key == "false":
    cookie = Cookie.SimpleCookie()
    cookie["COOKIE_TIME_STAMP"] = time.mktime(time.localtime(time.time()))
    os.environ["HTTP_COOKIE"] = str(cookie)

    cookie = Cookie.SmartCookie()
    cookie.load(os.environ["HTTP_COOKIE"])
    for key, item in cookie.items():
      if key == "COOKIE_TIME_STAMP":
        latest_cookie_time_stamp = item.value
        shell('echo "in else" >> /usr/apache/html/abc')
        break;
      else:
        latest_cookie_time_stamp = 0

    return latest_cookie_time_stamp

######################################################

var_cookie_time_stamp = get_cookie();
0
Comment
Question by:lin100
  • 3
3 Comments
 
LVL 5

Accepted Solution

by:
EOL earned 50 total points
ID: 11879623
See your python script runs on your python interpreter. This interpreter gotten his own environment from the os when the os executed your interpreter. Now your script changes the environment of your interpreter, but when the interpreter process is over, this environment is gone too.

I'd propose changing
cookie.load(os.environ["HTTP_COOKIE"])
to
incookie = file( 'cookie' )
cookie.load( incookie.read() )
incookie.close()

and
 os.environ["HTTP_COOKIE"] = str(cookie)
to
 outcookie = file( 'cookie' )
 outcookie.write( str(cookie) )
 outcookie.close()

but of course there's plenty of other possibilities to save your state, some of which depend on what webserver environment exist. Just think about pickle, shelve, marshall, anydbm, dbhash, mod_python, zope, webware, mysql, and so on.
0
 
LVL 5

Expert Comment

by:EOL
ID: 11879626
( ups, error: outcookie = file( 'cookie', 'w' ), of course )
0
 
LVL 5

Expert Comment

by:EOL
ID: 11879634
on a sidenote, in certain python-environments ( like mod_python ) the interpreter process isn't restarted between requests. But assuming that your cookie is still in the environment is bad because the webserver in multi-process/thread mode does not guarantee that he serves each request from the same user with the same thread, and even with a single thread, the webserver can still shut down the interpreter for various reasons, and restart it.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python tuples 2 133
Python script details 2 85
BASH script to modify crontab? 3 94
python - find anything after $ question. 9 47
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question