title: check whether a session cookie is still alive (Python)
body: <p>Please take a look at the following  <strong>python</strong> code snippet :</p>
  
  <pre><code>import cookielib, urllib, urllib2
  
  def login(username, password):
      cookie_jar = cookielib.LWPCookieJar()
      opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
      login = urllib.urlencode({'username': username, 'password': password})
      try:
          login_data = opener.open("http://www.example.com/login.php", login).read()
      except IOError:
          return 'Network Error'
  
      # on successful login the I'm storing the 'SESSION COOKIE'
      # that the site sends on a local file called cookie.txt
  
      cookie_jar.save('./cookie.txt', True, True)
      return login_data
  
  # this method is called after quite sometime
  # from calling the above method "login" 
  
  def re_accessing_the _site():
      cookie_jar = cookielib.LWPCookieJar()
  
      # Here I'm re-loading the saved cookie
      # from the file to the cookie jar
  
      cookie_jar.revert('./cookie.txt', True, True)
  
      # there's only 1 cookie in the cookie jar
      for Cookie in cookie_jar:
          print 'Expires : ', Cookie.expires  ## prints None
          print 'Discard : ', Cookie.discard  ## prints True , means that the cookie is a
                                              ## session cookie
          print 'Is Expired : ', Cookie.is_expired()  ## prints False
  
      opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
      try:
          data = opener.open("http://www.example.com/send.php")
  
          # Sometimes the opening fails as the cookie has expired
          # &amp; sometimes it doesn't. Here I want a way to determine
          # whether the (session) cookie is still alive
  
      except IOError:
          return False
      return True
  </code></pre>
  
  <p>First I'm calling the method <strong>login</strong> &amp; saves the retrieved cookie (which is a <strong>session cookie</strong>)to a local file called <strong>cookie.txt</strong>.
  Next after quite sometime (<em>15-20 mins</em>) I'm calling the other method <strong>re_accessing_the _site</strong> . This time I'm also re loading the previously saved cookie into the cookie jar.
  Sometimes it's working fine but sometimes it's blocking me to access (<strong>as the session cookie has expired</strong>) . So all I need is a way to check whether the cookie is still alive during the call ...</p>