• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Linux environment does not recalled stored value in the os environment

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
lin100
Asked:
lin100
  • 3
1 Solution
 
EOLCommented:
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
 
EOLCommented:
( ups, error: outcookie = file( 'cookie', 'w' ), of course )
0
 
EOLCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now