Solved

Linux environment does not recalled stored value in the os environment

Posted on 2004-08-23
3
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

687 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