Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # import
- from twisted.internet import protocol, reactor
- from twisted.protocols import basic
- from twisted.python import log
- from time import strftime
- import MySQLdb
- print "Englorecy Server"
- print
- class User:
- username = ""
- instance = None
- class ServerReceiver(basic.LineReceiver):
- uiUserList = []
- uiUserListSent = []
- delimiter = "\0"
- def connectionMade(self):
- self.factory.clients.append(self)
- print "connected"
- def connectionLost(self, reason):
- self.factory.clients.remove(self)
- print "disconnected"
- def lineReceived(self, line):
- self.evaluate(line)
- def populateUserInfo(self):
- temp = User()
- temp.instance = self
- def evaluate(self, string):
- line = string.split(":", 1)
- if line[0] == "function":
- command = line[1].split(":", 1)
- try:
- parameters = command[1].split(" ")
- argList = []
- try:
- for p in parameters:
- argList.append(p)
- except:
- argList = []
- except:
- argList = []
- #self.database_openconnection()
- if len(argList) == 0:
- try:
- print "Command received:", command[0]+"()"
- eval("self."+command[0]+"()")
- except NameError: print "No such command"; pass
- elif len(argList) == 1:
- try:
- print "Command received:", command[0]+"('"+parameters[0]+"')"
- eval("self."+command[0]+"('"+parameters[0]+"')")
- except NameError: print "No such command:"; pass
- elif len(argList) == 2:
- try:
- print "Command received:", command[0]+"('"+parameters[0]+"','"+parameters[1]+"')"
- eval("self."+command[0]+"('"+parameters[0]+"','"+parameters[1]+"')")
- except NameError: print "No such command", command[0]+"('"+parameters[0]+"','"+parameters[1]+"')"; pass
- elif len(argList) == 3:
- try:
- print "Command received:", command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"')"
- eval("self."+command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"')")
- except NameError: print "No such command"; pass
- elif len(argList) == 4:
- try:
- print "Command received:", command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"','"+parameters[3]+"')"
- eval("self."+command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"','"+parameters[3]+"')")
- except NameError: print "No such command"; pass
- elif len(argList) == 5:
- try:
- print "Command received:", command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"','"+parameters[3]+"','"+parameters[4]+"')"
- eval("self."+command[0]+"('"+parameters[0]+"','"+parameters[1]+"','"+parameters[2]+"','"+parameters[3]+"','"+parameters[4]+"')")
- except NameError: print "No such command"; pass
- #self.database_closeconnection()
- if line[0] == "chat":
- user = line[1].split(":", 1)[0]
- msg = line[1].split(":", 1)[1]
- print "[%s] %s: %s" % (strftime("%H:%M:%S"), user, msg)
- for client in self.factory.clients:
- client.sendLine("%s: %s" % (user, msg))
- def login_request(self, un, passwd):
- if "';" not in passwd:
- results = self.database_execute("SELECT username, password FROM login WHERE username='%s' AND password='%s'" % (un, passwd))
- res = []
- for r in results:
- for i in r:
- res.append(i)
- if un and passwd in res:
- self.sendLine("login_answer")
- self.uiUserList.append(un)
- else:
- self.sendLine("login_failed")
- res = []
- else: self.sendLine("SECURITY BREACH!")
- def welcome_request(self):
- self.sendLine("<span style='color: #0000FF; font-weight: bold;'>Welcome to Englorecy Online!\nEnjoy your stay!</span>")
- def handle_broadcast(self, msg):
- print "*", msg
- def handle_uiuserlist(self):
- self.sendLine("userlist:clear:___clear___")
- for user in self.uiUserList:
- if user not in self.uiUserListSent:
- self.sendLine("userlist:%s" % user)
- self.uiUserListSent.append(user)
- else:
- continue
- def database_openconnection(self):
- try:
- self.databaseConnection = MySQLdb.Connect(host="127.0.0.1", port=3306, user="englorecy", passwd="12QWaszx", db="englorecy")
- self.databaseCursor = self.databaseConnection.cursor()
- except:
- print "MySQL Connection Error!"
- else:
- print "MySQL Connection Established!"
- def database_closeconnection(self):
- try:
- self.databaseConnection.close()
- except:
- print "Couldn't close MySQL connection."
- else:
- print "MySQL connection closed"
- def database_execute(self, sql):
- try:
- self.databaseCursor.execute(sql)
- except:
- print "SQL Syntax error detected!"
- else:
- return self.databaseCursor.fetchall()
- class XMLSocket(protocol.ServerFactory):
- clients = []
- def __init__(self, protocol=None):
- self.protocol = protocol
- def main(port=717):
- reactor.listenTCP(port, XMLSocket(ServerReceiver))
- print "Listening to port %s..." % port
- print "Starting Server Reactor"
- reactor.run()
- print "Shutting down the server..."
- if __name__=='__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement