Skip to content

Commit f855634

Browse files
committed
Added server functionality
1 parent e0f230a commit f855634

1 file changed

Lines changed: 28 additions & 73 deletions

File tree

Lines changed: 28 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#Key Generation Service
22
import sys
3-
import os.path
43
import os,binascii
5-
import shelve
4+
import socket
65
sys.path.append("../../..")
76
from ColorText import ColorText
7+
sys.path.append("../../DataBase")
8+
from DataBase import DataBase
89
class Explanation(object):
910

1011
def explanation1(self):
@@ -22,75 +23,15 @@ def explanation4(self):
2223
print "This system needs to make sure that it doesn't hand out same key to different servers"
2324
print "For this it needs to have a proper lock over internal database"
2425

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-
8626
class EncodeURL(object):
8727
def __init__(self):
8828
self.ct = ColorText()
8929
self.exp = Explanation()
9030
self.db = DataBase()
91-
self.unUsedKeys = "unUsedKeys.txt"
92-
self.usedKeys = "usedKeys.txt"
31+
self.unUsedKeys = "../../DataBase/unUsedKeys.txt"
32+
self.usedKeys = "../../DataBase/usedKeys.txt"
9333
self.cachedKeys = []
34+
self.s = socket.socket()
9435

9536
def generateKeysWriteToDB(self):
9637
keys = ""
@@ -106,20 +47,34 @@ def bringKeysToCache(self):
10647
self.db.writeToHash(self.usedKeys, key, "True")
10748
print self.cachedKeys
10849

109-
def run(self, string):
110-
self.ct.display(4*"\t"+"Approch2", "black-highlight")
111-
self.exp.explanation1()
50+
def returnKey(self):
11251
if not (self.db.ifFile(self.unUsedKeys)):
52+
self.exp.explanation1()
11353
self.generateKeysWriteToDB()
114-
self.exp.explanation2()
11554
if not self.cachedKeys:
55+
self.exp.explanation2()
11656
self.bringKeysToCache()
11757
self.exp.explanation4()
11858
return self.cachedKeys.pop()
119-
120-
121-
12259

60+
def server(self):
61+
port = 5
62+
self.s.bind(('', port))
63+
self.s.listen(5)
64+
print "Started server"
65+
66+
def run(self):
67+
self.ct.display(4*"\t"+"Approch2", "black-highlight")
68+
#Start the server
69+
self.server()
70+
while True:
71+
key = self.returnKey()
72+
#Send key to client
73+
c, addr = self.s.accept()
74+
c.send(key)
75+
print "Sent key ",key, " to client ", addr
76+
c.close()
77+
12378
#Main
12479
obj = EncodeURL()
125-
obj.run("google.com")
80+
obj.run()

0 commit comments

Comments
 (0)