Solved

Linux environment does not recalled stored value in the os environment

Posted on 2004-08-23
3
265 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
Comment Utility
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
Comment Utility
( ups, error: outcookie = file( 'cookie', 'w' ), of course )
0
 
LVL 5

Expert Comment

by:EOL
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now