1 #!/usr/bin/env python
   2 import sys
   3 import memcache
   4 import time
   5 
   6 # license note
   7 # Copyright (c) 2014, Eliezer Croitoru
   8 # All rights reserved.
   9 #
  10 # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  11 #
  12 # 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  13 #
  14 # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  15 #
  16 # 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  17 #
  18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  19 
  20 memc = memcache.Client(['127.0.0.1:11211'], debug=0);
  21 
  22 #set debug mode for True or False
  23 debug = False
  24 #set time for login to be usable in minutes
  25 logintime = 30
  26 
  27 while True:
  28      line = sys.stdin.readline().strip()
  29      arr = line.split()
  30      msg = ""
  31      if len(arr) == 3 and arr[2] and arr[2] == "LOGIN":
  32         memc.set(arr[1],  time.time())
  33         msg = "Welcome"
  34      if len(arr) == 3 and arr[2] and arr[2] == "LOGOUT":
  35         memc.set(arr[1],  0)
  36         msg = "GoodBy"
  37 
  38      client = memc.get(arr[1])
  39 
  40      if debug:
  41        sys.stderr.write("__debug info__" + str(time.time()) +": \"" + line + "\"\n")
  42      if client and time.time() - time.mktime(time.gmtime(client)) < int(time.time() - logintime*60):
  43         sys.stdout.write(arr[0] + " OK " + msg + "\n")
  44         if debug:
  45           sys.stderr.write("__debug info__ : \"" + line + "\" and time in db is: "+ str(client) +"\n")
  46      else:
  47         sys.stdout.write(arr[0] + " ERR " + msg + "\n")
  48         if debug:
  49           sys.stderr.write("__debug info__ : " + line + '\n')
  50      sys.stdout.flush()

EliezerCroitoru/SessionHelper/Python (last edited 2014-09-03 19:31:25 by Eliezer Croitoru)