|
| 1 | +#Key Generation Service |
| 2 | +import sys |
| 3 | +import os.path |
| 4 | +import os,binascii |
| 5 | +import shelve |
| 6 | +sys.path.append("../../..") |
| 7 | +from ColorText import ColorText |
| 8 | +class Explanation(object): |
| 9 | + |
| 10 | + def explanation1(self): |
| 11 | + print "So in this approch we first start with generating unique 8 character keys offline and store it in database" |
| 12 | + |
| 13 | + def explanation2(self): |
| 14 | + print "When ever we need a key we first look into cache if it has kesy then we give the client that key" |
| 15 | + print "Else we take some pregenerated keys from database and bring them in cache" |
| 16 | + |
| 17 | + def explanation3(self): |
| 18 | + print "As soon as the keys are brought into cache we mark them as used in database" |
| 19 | + print "This ensures that each server gets unique keys" |
| 20 | + |
| 21 | + def explanation4(self): |
| 22 | + print "This system needs to make sure that it doesn't hand out same key to different servers" |
| 23 | + print "For this it needs to have a proper lock over internal database" |
| 24 | + |
| 25 | + |
| 26 | +class DataBase(object): |
| 27 | + |
| 28 | + def isEmpty(self, fileName): |
| 29 | + file_object = open(fileName, "r") |
| 30 | + for line in file_object: |
| 31 | + if(line.strip() != ""): |
| 32 | + file_object.close() |
| 33 | + return False |
| 34 | + file_object.close() |
| 35 | + return True |
| 36 | + |
| 37 | + def ifFile(self,fileName): |
| 38 | + if not os.path.isfile(fileName): |
| 39 | + #print "File does not exist" |
| 40 | + return False |
| 41 | + return True |
| 42 | + |
| 43 | + def write(self, fileName, string): |
| 44 | + file_object = open(fileName, "a") |
| 45 | + file_object.write(string+"\n") |
| 46 | + file_object.close() |
| 47 | + |
| 48 | + def read(self, fileName): |
| 49 | + if not self.ifFile(fileName): |
| 50 | + return |
| 51 | + file_object = open(fileName, "r") |
| 52 | + for line in file_object: |
| 53 | + print line, |
| 54 | + file_object.close() |
| 55 | + |
| 56 | + def getKey(self, fileName): |
| 57 | + if not self.ifFile(fileName): |
| 58 | + return |
| 59 | + file_object = open(fileName, 'r') |
| 60 | + lines = file_object.readlines() |
| 61 | + file_object.close() |
| 62 | + i = 0 |
| 63 | + while(lines[i].strip() == ""): |
| 64 | + i = i+1 |
| 65 | + key = lines[i] |
| 66 | + file_object = open(fileName, 'w') |
| 67 | + file_object.write(''.join(lines[i+1:])) |
| 68 | + file_object.close() |
| 69 | + #if file empty then delete it |
| 70 | + if(self.isEmpty(fileName)): |
| 71 | + os.remove(fileName) |
| 72 | + return key |
| 73 | + |
| 74 | + def writeToHash(self, fileName, key, value): |
| 75 | + hashFile_object = shelve.open(fileName) |
| 76 | + hashFile_object[key] = value |
| 77 | + hashFile_object.close() |
| 78 | + |
| 79 | + def readFromHash(self, fileName, key): |
| 80 | + hashFile_object = shelve.open(fileName) |
| 81 | + if (key in hashFile_object): |
| 82 | + value = hashFile_object[key] |
| 83 | + hashFile_object.close() |
| 84 | + return value |
| 85 | + |
| 86 | +class EncodeURL(object): |
| 87 | + def __init__(self): |
| 88 | + self.ct = ColorText() |
| 89 | + self.exp = Explanation() |
| 90 | + self.db = DataBase() |
| 91 | + self.unUsedKeys = "unUsedKeys.txt" |
| 92 | + self.usedKeys = "usedKeys.txt" |
| 93 | + self.cachedKeys = [] |
| 94 | + |
| 95 | + def generateKeysWriteToDB(self): |
| 96 | + keys = "" |
| 97 | + for i in range(10): |
| 98 | + keys = keys + str(binascii.b2a_hex(os.urandom(4)))+"\n" |
| 99 | + self.db.write(self.unUsedKeys, keys) |
| 100 | + |
| 101 | + def bringKeysToCache(self): |
| 102 | + self.exp.explanation3() |
| 103 | + for i in range(5): |
| 104 | + key = self.db.getKey(self.unUsedKeys)[:-1] |
| 105 | + self.cachedKeys.append(key) |
| 106 | + self.db.writeToHash(self.usedKeys, key, "True") |
| 107 | + print self.cachedKeys |
| 108 | + |
| 109 | + def run(self, string): |
| 110 | + self.ct.display(4*"\t"+"Approch2", "black-highlight") |
| 111 | + self.exp.explanation1() |
| 112 | + if not (self.db.ifFile(self.unUsedKeys)): |
| 113 | + self.generateKeysWriteToDB() |
| 114 | + self.exp.explanation2() |
| 115 | + if not self.cachedKeys: |
| 116 | + self.bringKeysToCache() |
| 117 | + self.exp.explanation4() |
| 118 | + return self.cachedKeys.pop() |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +#Main |
| 124 | +obj = EncodeURL() |
| 125 | +obj.run("google.com") |
0 commit comments