Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.18 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # import
  3. from twisted.internet import protocol, reactor
  4. from twisted.protocols import basic
  5. from twisted.python import log
  6. from time import strftime
  7. import MySQLdb
  8.  
  9. print "Englorecy Server"
  10. print
  11.  
  12. class User:
  13.     username = ""
  14.     instance = None
  15.  
  16. class ServerReceiver(basic.LineReceiver):
  17.    
  18.     uiUserList = []
  19.     uiUserListSent = []
  20.    
  21.     delimiter = "\0"
  22.    
  23.     def connectionMade(self):
  24.         self.factory.clients.append(self)
  25.         print "connected"
  26.    
  27.     def connectionLost(self, reason):
  28.         self.factory.clients.remove(self)
  29.         print "disconnected"
  30.    
  31.     def lineReceived(self, line):
  32.         self.evaluate(line)
  33.        
  34.     def populateUserInfo(self):
  35.         temp = User()
  36.         temp.instance = self
  37.        
  38.    
  39.     def evaluate(self, string):
  40.         line = string.split(":", 1)
  41.         if line[0] == "function":
  42.             command = line[1].split(":", 1)
  43.             try:
  44.                 parameters = command[1].split(" ")
  45.                 argList = []
  46.                 try:
  47.                     for p in parameters:
  48.                         argList.append(p)
  49.                 except:
  50.                     argList = []
  51.             except:
  52.                 argList = []
  53.            
  54.             #self.database_openconnection()
  55.            
  56.             if len(argList) == 0:
  57.                 try:
  58.                     print "Command received:", command[0]+"()"
  59.                     eval("self."+command[0]+"()")
  60.                 except NameError: print "No such command"; pass
  61.            
  62.             elif len(argList) == 1:
  63.                 try:
  64.                     print "Command received:", command[0]+"('"+parameters[0]+"')"
  65.                     eval("self."+command[0]+"('"+parameters[0]+"')")
  66.                 except NameError: print "No such command:"; pass
  67.                
  68.             elif len(argList) == 2:
  69.                 try:
  70.                     print "Command received:", command[0]+"('"+parameters[0]+"','"+parameters[1]+"')"
  71.                     eval("self."+command[0]+"('"+parameters[0]+"','"+parameters[1]+"')")
  72.                 except NameError: print "No such command", command[0]+"('"+parameters[0]+"','"+parameters[1]+"')"; pass
  73.                
  74.             elif len(argList) == 3:
  75.                 try:
  76.                     print "Command received:", command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"')"
  77.                     eval("self."+command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"')")
  78.                 except NameError: print "No such command"; pass
  79.                
  80.             elif len(argList) == 4:
  81.                 try:
  82.                     print "Command received:", command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"','"+parameters[3]+"')"
  83.                     eval("self."+command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"','"+parameters[3]+"')")
  84.                 except NameError: print "No such command"; pass
  85.                
  86.             elif len(argList) == 5:
  87.                 try:
  88.                     print "Command received:", command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"','"+parameters[3]+"','"+parameters[4]+"')"
  89.                     eval("self."+command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"','"+parameters[3]+"','"+parameters[4]+"')")
  90.                 except NameError: print "No such command"; pass
  91.                
  92.             #self.database_closeconnection()
  93.        
  94.         if line[0] == "chat":
  95.             user = line[1].split(":", 1)[0]
  96.             msg = line[1].split(":", 1)[1]
  97.             print "[%s] %s: %s" % (strftime("%H:%M:%S"), user, msg)
  98.             for client in self.factory.clients:
  99.                 client.sendLine("%s: %s" % (user, msg))
  100.    
  101.     def login_request(self, un, passwd):
  102.         if "';" not in passwd:
  103.             results = self.database_execute("SELECT username, password FROM login WHERE username='%s' AND password='%s'" % (un, passwd))
  104.             res = []
  105.             for r in results:
  106.                 for i in r:
  107.                     res.append(i)
  108.             if un and passwd in res:
  109.                 self.sendLine("login_answer")
  110.                 self.uiUserList.append(un)
  111.             else:
  112.                 self.sendLine("login_failed")
  113.             res = []
  114.         else: self.sendLine("SECURITY BREACH!")
  115.    
  116.     def welcome_request(self):
  117.         self.sendLine("<span style='color: #0000FF; font-weight: bold;'>Welcome to Englorecy Online!\nEnjoy your stay!</span>")
  118.    
  119.     def handle_broadcast(self, msg):
  120.         print "*", msg
  121.    
  122.     def handle_uiuserlist(self):
  123.         self.sendLine("userlist:clear:___clear___")
  124.         for user in self.uiUserList:
  125.             if user not in self.uiUserListSent:
  126.                 self.sendLine("userlist:%s" % user)
  127.                 self.uiUserListSent.append(user)
  128.             else:
  129.                 continue
  130.        
  131.     def database_openconnection(self):
  132.         try:
  133.             self.databaseConnection = MySQLdb.Connect(host="127.0.0.1", port=3306, user="englorecy", passwd="12QWaszx", db="englorecy")
  134.             self.databaseCursor = self.databaseConnection.cursor()
  135.         except:
  136.             print "MySQL Connection Error!"
  137.         else:
  138.             print "MySQL Connection Established!"
  139.    
  140.     def database_closeconnection(self):
  141.         try:
  142.             self.databaseConnection.close()
  143.         except:
  144.             print "Couldn't close MySQL connection."
  145.         else:
  146.             print "MySQL connection closed"
  147.    
  148.     def database_execute(self, sql):
  149.         try:
  150.             self.databaseCursor.execute(sql)
  151.         except:
  152.             print "SQL Syntax error detected!"
  153.         else:
  154.             return self.databaseCursor.fetchall()
  155.  
  156. class XMLSocket(protocol.ServerFactory):
  157.     clients = []
  158.    
  159.     def __init__(self, protocol=None):
  160.         self.protocol = protocol
  161.  
  162. def main(port=717):
  163.     reactor.listenTCP(port, XMLSocket(ServerReceiver))
  164.     print "Listening to port %s..." % port
  165.     print "Starting Server Reactor"
  166.     reactor.run()
  167.     print "Shutting down the server..."
  168.    
  169. if __name__=='__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement