Solved

Linux environment does not recalled stored value in the os environment

Posted on 2004-08-23
3
269 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python algorithm 20 150
Visualizing tuple values in Python (or generally) 3 74
BASH script to modify crontab? 3 67
Modify a small python script 19 109
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

810 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