server questions

maximilianscherr MaximilianScherr at T-Online.de
Sun Mar 10 07:30:51 EST 2002


hi,

ok at first my code (questions at the bottom):

import os.path
import os
import threading
import SocketServer
import binascii
import string
import socket

class Program:
    def Start(self):
        ucisfile = os.path.isfile('uoslserver.cfg')
        pcinfohandler = PCInfoHandler()
        pcinfohandler.CheckFile()
        ohandler = OHandler()
        ohandler.CheckFile()
        if ucisfile == 0:
            ohandler.WriteO('Couldn\'t find uoslserver.cfg!')

        elif ucisfile == 1:
            readuc = open('uoslserver.cfg')
            try:
                address = eval(readuc.readline())
                try:
                    ohandler.WriteO('Starting up...')
                    ihandler = IHandler()
                    serverthread = Server(address)
                    serverthread.start()
                    ihandlerthread = threading.Thread(None, 
ihandler.ReadI, None, (serverthread,))
                    ihandlerthread.start()
                    ohandler.WriteO('...finished!')
                    
                except:
                    ohandler.WriteO('...aborted!')
                    
            except:
                ohandler.WriteO('Couldn\'t read uoslserver.cfg')

class OHandler:
    def CheckFile(self):
        ulisfile = os.path.isfile('uoslserver.log')
        if ulisfile == 0:
            writeul = open('uoslserver.log', 'w')
            writeul.close()

        elif ulisfile == 1:
            os.remove('uoslserver.log')
            writeul = open('uoslserver.log', 'w')
            writeul.close()

    def WriteO(self, string):
        writeul = open('uoslserver.log', 'a')
        print string
        writeul.write(string +'\n')
        writeul.close()

class Server(threading.Thread):
    def __init__(self, address):
        self.serverstate = 1
        self.address = address
        threading.Thread.__init__(self, None, self.Start)
        
    def Start(self):
        self.server = SocketServer.ThreadingTCPServer(self.address, 
RequestHandler)
        while self.serverstate == 1:
            self.server.handle_request()
            
    def Save(self):
        pcinfohandler = PCInfoHandler()
        pcinfohandler.SetInfoAll()
        
    def ShutDown(self):
        self.serverstate = 0
        functionhandler = FunctionHandler()
        for op in functionhandler.ListOnlinePlayers():
            op.Close()
            
        sdsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sdsock.connect(self.address)
        sdsock.close()
        
class RequestHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        clienthandlerthread = ClientHandler(self.request)
        clienthandlerthread.start()
        
class ClientHandler(threading.Thread):
    def __init__(self, sockobj):
        self.clienthandlerstate = 1
        self.sockobj = sockobj
        threading.Thread.__init__(self, None, self.Recv)
        
    def Recv(self):
        datahandler = DataHandler()
        pcinfohandler = PCInfoHandler()
        while self.clienthandlerstate == 1 and self.sockobj:
            try:
                data = self.sockobj.recv(1024)
                returnvalue = datahandler.Start(data, self)
                if returnvalue != 0:
                    self.pcserial = returnvalue[0]
                    self.pcname = returnvalue[1]
                    self.pcpass = returnvalue[2]
                    self.pcx = returnvalue[3]
                    self.pcy = returnvalue[4]
                    self.pcz = returnvalue[5]
                    self.pcdir = returnvalue[6]
                    
            except:
                self.clienthandlerstate = 0
                pcinfohandler.SetInfoSingle(self.pcserial, 
self.pcname, self.pcpass, self.pcx, self.pcy, self.pcz, self.pcdir)
                ohandler = OHandler()
                ohandler.WriteO('Player '+ self.pcname +' logged 
off!')
                
    def Close(self):
        self.sockobj.close()
        self.clienthandlerstate = 0
        
class DataHandler:
    def Start(self, data, clienthandlerthread):
        self.converteddata = binascii.b2a_hex(data)
        self.clienthandlerthread = clienthandlerthread
        if self.converteddata[:4] == 'fe01':
            returnvalue = self.HandlePacketfe01()
            return returnvalue

        else:
            return 0
        
    def HandlePacketfe01(self):
        conversionhandler = ConversionHandler()
        pcserial = conversionhandler.ConvertHexToInt
(self.converteddata[8:16])
        pcname = conversionhandler.ConvertHexToStr(self.converteddata
[24:340])
        pcpass = conversionhandler.ConvertHexToStr(self.converteddata
[1116:1190])
        pcinfohandler = PCInfoHandler()
        returnvalue = pcinfohandler.GetInfo(pcserial)
        functionhandler = FunctionHandler()
        ohandler = OHandler()
        if returnvalue != 0:
            if functionhandler.CheckPass(pcserial, pcpass) == 0:
                self.clienthandlerthread.sockobj.close()
                self.clienthandlerthread.clienthandlerstate = 0
                return 0
            
            elif functionhandler.CheckPass(pcserial, pcpass) == 1:
                ohandler.WriteO('Player '+ pcname +' logged on from 
client address: '+ str(self.clienthandlerthread.sockobj.getsockname
()))
                return returnvalue
            
        elif returnvalue == 0:
            if functionhandler.CheckPass(pcserial, pcpass) == 0:
                self.clienthandlerthread.sockobj.close()
                self.clienthandlerthread.clienthandlerstate = 0
                return 0
            
            elif functionhandler.CheckPass(pcserial, pcpass) == 1:
                ohandler.WriteO('Player '+ pcname +' logged on from 
client address: '+ str(self.clienthandlerthread.sockobj.getsockname
()))
                return [pcserial, pcname, pcpass, 554, 576, 0, 0]
            
class FunctionHandler:
    def CheckPass(self, pcserial, pcpass):
        pcinfohandler = PCInfoHandler()
        returnvalue = pcinfohandler.GetInfo(pcserial)
        if returnvalue != 0:
            for pcinfo in returnvalue:
                for op in self.ListOnlinePlayers():
                    if op.pcpass != pcpass or pcinfo[2] != pcpass:
                        return 0
                    
                    elif pcinfo[2] == pcpass:
                        return 1
                    
        elif returnvalue == 0 and self.FindOnlinePlayerByPCSerial
(pcserial) != 0:
            return 0

        elif returnvalue == 0 and self.FindOnlinePlayerByPCSerial
(pcserial) == 0:
            return 1
        
    def ListOnlinePlayers(self):
        oplist = []
        for threadobj in threading.enumerate():
            try:
                threadobj.pcserial
                oplist.append(threadobj)
                
            except:
                pass
            
        return oplist

    def FindOnlinePlayerByPCSerial(self, pcserial):
        if self.ListOnlinePlayers() == []:
            return 0

        else:
            for op in self.ListOnlinePlayers():
                if op.pcserial == pcserial:
                    return op

                else:
                    return 0
                
class PCInfoHandler:
    def CheckFile(self):
        pdisfile = os.path.isfile(os.getcwd() +'\\data\\pc.dat')
        if pdisfile == 0:
            writepd = open(os.getcwd() +'\\data\\pc.dat', 'w')
            writepd.write('[]')
            writepd.close()
        
    def GetInfo(self, pcserial):
        readpd = open(os.getcwd() +'\\data\\pc.dat', 'r')
        pcinfolist = readpd.readline()
        readpd.close()
        try:
            pcinfolist = eval(pcinfolist)
            if pcinfolist == []:
                return 0
            
            else:
                for pcinfo in pcinfolist:
                    if pcinfo[0] != pcserial:
                        return 0
                    
                    elif pcinfo[0] == pcserial:
                        return pcinfo
                    
        except:
            ohandler = OHandler()
            ohandler.WriteO('Couldn\'t read pc.dat')
            
    def SetInfoSingle(self, pcserial, pcname, pcpass, pcx, pcy, pcz, 
pcdir):
        readpd = open(os.getcwd() +'\\data\\pc.dat', 'r')
        pcinfolist = readpd.readline()
        readpd.close()
        try:
            pcinfolist = eval(pcinfolist)
            for pcinfo in pcinfolist:
                if pcinfo[0] != pcserial:
                    pcinfolist.append[[pcserial, pcname, pcpass, pcx, 
pcy, pcz, pcdir]]

                elif pcinfo[0] == pcserial:
                    pcinfolist.remove(pcinfo)
                    pcinfolist.append[[pcserial, pcname, pcpass, pcx, 
pcy, pcz, pcdir]]

        except:
            ohandler = OHandler()
            ohandler.WriteO('Couldn\'t read pc.dat')
            
        writepd = open(os.getcwd() +'\\data\\pc.dat', 'w')
        writepd.write(str(pcinfolist))
        writepd.close()
        
    def SetInfoAll(self):
        readpd = open(os.getcwd() +'\\data\\pc.dat', 'r')
        pcinfolist = readpd.readline()
        readpd.close()
        try:
            pcinfolist = eval(pcinfolist)
            for pcinfo in pcinfolist:
                functionhandler = FunctionHandler()
                for op in functionhandler.ListOnlinePlayers():
                    if op.getName() == pcinfo[0]:
                        pcinfolist.remove(pcinfo)
                        pcinfolist.append[[op.pcserial, op.pcname, 
op.pcpass, op.pcx, op.pcy, op.pcz, op.pcdir]]

        except:
            ohandler = OHandler()
            ohandler.WriteO('Couldn\'t read pc.dat')
            
        writepd = open(os.getcwd() +'\\data\\pc.dat', 'w')
        writepd.write(str(pcinfolist))
        writepd.close()
        
class IHandler:
    def ReadI(self, serverthread):
        ihandlerstate = 1
        ohandler = OHandler()
        functionhandler = FunctionHandler()
        while ihandlerstate == 1:
            input = raw_input()
            if input == 'c':
                ohandler.WriteO('Input: c')
                ohandler.WriteO('Valid commands:')
                ohandler.WriteO('c -- show this list')
                ohandler.WriteO('o -- show online players')
                ohandler.WriteO('a -- save')
                ohandler.WriteO('s -- shut down')
                
            elif input == 'o':
                ohandler.WriteO('Input: o')
                ohandler.WriteO('Online players:')
                opstr = ''
                for op in functionhandler.ListOnlinePlayers():
                    if len(opstr) < len
(functionhandler.ListOnlinePlayers()) - 1:
                        opstr = opstr + op.pcname +', '

                    else:
                        opstr = opstr + op.pcname

                if opstr != '':
                    ohandler.WriteO(opstr)

                elif opstr == '':
                    ohandler.WriteO('No players are online!')
                    
            elif input == 'a':
                ohandler.WriteO('Input: a')
                ohandler.WriteO('Saving...')
                serverthread.Save()
                ohandler.WriteO('...finished!')
                
            elif input == 's':
                ohandler.WriteO('Input: s')
                ohandler.WriteO('Shutting down...')
                serverthread.ShutDown()
                ihandlerstate = 0
                ohandler.WriteO('...finished!')
                
class ConversionHandler:
    def ConvertHexToInt(self, hex):
        while hex[0] == '0':
            hex = hex[1:]
            
        int = string.atoi(hex, 16)
        return int
    
    def ConvertHexToStr(self, hex):
        hex = string.replace(hex, '00', '')
        str = binascii.a2b_hex(hex)
        return str
    
program = Program()
program.Start()

ok now the two questions i have at the moment are: 

1)how can i shut dwon the server right? (see Server class)
2)how can i find out right if i a client disconected? (see 
ClientHandler class)

thanks in advance,

Max

P.S. I know it's a very very bad scripted:), you don't need to tell 
me that:)





More information about the Python-list mailing list