Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. #!/usr/bin/python
  2. # server rewrite
  3.  
  4. import sys, threading, SocketServer
  5. from time import ctime, sleep, time
  6. usersInfo = open("users/users.txt", "r")
  7. logs = open("logs/main.log", "w")
  8. adminLog = open("logs/admin.log","w")
  9. online = []
  10. threads = []
  11.  
  12. class ClientAuth(SocketServer.BaseRequestHandler):
  13.     def handle(self):
  14.         self.data = self.request.recv(1024)
  15.         # ip is found with self.client_address[0]
  16.         self.data = self.data.split()
  17.         inUser = self.data[0]
  18.         inPassWd = self.data[1]
  19.         userList = self._getUserList()
  20.         try:
  21.             SvPassWd = userList[inUser]
  22.             self._auth(inPassWd, SvPassWd)
  23.         except KeyError:
  24.             print "failed login from %s at %s" % (self.client_address[0], ctime())
  25.             logs.write("failed login from %s at %s" % (self.client_address[0], ctime()))
  26.  
  27.     def _auth(self, inPassWd, SvPassWd):
  28.         if inPassWd == SvPassWd:
  29.             print "user login from %s at %s" % (self.client_address[0], ctime)
  30.             logs.write("user login from %s at %s" % (self.client_address[0], ctime))
  31.             clientHandler(self)
  32.         else:
  33.             print "failed login from %s at %s" % (self.client_address[0], ctime)
  34.             logs.write("failed login from %s at %s" % (self.client_address[0], ctime))
  35.  
  36.     def _getUserList(self):
  37.         userList = []
  38.         line = usersInfo.readline()
  39.         while line:
  40.             if len(line) > 1:
  41.                 print line
  42.                 fileUser = line.split(',')[0][2:-1]
  43.                 filePass = line.split(',')[1][2:-3]
  44.                 infoTpl = (fileUser, filePass)
  45.                 print infoTpl
  46.                 userList.append(infoTpl)
  47.             line = usersInfo.readline()
  48.         print dict(userList)
  49.         return dict(userList)
  50.  
  51. class clientHandler:
  52.     def __init__(self, selfSocket):
  53.         self.serverSocket = selfSocket
  54.         print self.serverSocket.client_address
  55.  
  56. if __name__ == "__main__":
  57.     HOST, PORT = "localhost", 10001
  58.     print HOST , PORT
  59.  
  60.     # Create the server, binding to localhost on port 9999
  61.     server = SocketServer.TCPServer((HOST, PORT), ClientAuth)
  62.  
  63.     # Activate the server; this will keep running until you
  64.     # interrupt the program with Ctrl-C
  65.     server.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement