I don't really know how to word what I want to ask so please bear with me.

I'm making a program that will start up a game server written in Java and read the outputs and edit the packets so that I can modify this game. It will read outputs from the server just fine. Now I need to read and edit packets that are sent and received.

I'm looking at a whole list of packets that go from the client to the server and vice-versa. Like below,

Packet ID      Purpose       Field Description      Field Type
------------------------------------------------------------------------------
  0xFF        Disconnect         Packet ID            Byte
                                 Reason               String

I want to intercept this packet with and change the "Reason" to..I don't know...Bacon.

What kind of Python data you have got and code until now? Those inputs from server you have read? Are they list or what, you put only print of some kind of table (from game documentation?).

No, no. The table isn't code it's just part of a table listing what all of the packets do. I just have it there as an example of what I want to do, get the "Reason" from the packet and change it to something else.

Here is my code as of now. Some of it is not mine, copied from another script that handled a previous version of this game.

import os, time
import subprocess as sub


class AlphaServer(object):
    def __init__(self, process):
        self.process = process

    def say(self, message):
        self.stdin("say %s\n" % message)

    def stop(self):
        self.stdin("stop")

    def forceStop(self):
        self.process.terminate()

    def stdin(self, input):
        self.process.stdin.write(input)

    @property
    def stdout(self):
        return self.process.stdout.readline().strip()

    @property
    def stderr(self):
        return self.process.stderr.readline().strip()

class Output(object):
    def __init__(self, output):
        self.output = output.split()

    @property
    def DateTime(self):
        return self.output[0:2]

    @property
    def Type(self):
        return self.output[2]

    @property
    def Message(self):
        return " ".join(self.output[3:])

    @property
    def Player(self):
        if "".join(self.output[3::4]) == "WildBamaBoy":
            return "WildBamaBoy"
        else:
            return None

def read_config():
    """
        Read settings from Server Config.ini
    """

    try:
        if not os.path.isfile("Server Config.ini"):
            config      = open("Server Config.ini", "w")
            properties  = open("server.properties", "r").read()

            config.write("#################\n")
            config.write("Original Settings\n")
            config.write("#################\n\n")
            config.write(properties)

            config.write("\n###################\n")
            config.write("Additional Settings\n")
            config.write("###################\n")
            config.write("reserved_memory(MB) = 1024")
            config.close()

        config       = open("Server Config.ini", "r").read().splitlines()

        server_ip    = config[6][10:].rstrip()
        server_port  = config[7][12:].rstrip()
        level_name   = config[8][11:].rstrip()
        reserved_mem = config[13][22:].rstrip()

        print "Server IP:",           server_ip
        print "Server Port:",         server_port
        print "Level Name:",          level_name
        print "Reserved Memory(MB):", reserved_mem
        print "----------------------------------------\n"

        return server_ip, server_port, level_name, reserved_mem

    except Exception as error:
        print "Error occured while reading configuration: %s" % error
        time.sleep(1)
        raise SystemExit


def StartServer():
    """
        Start up the server and read outputs.
    """

    config = read_config()
    startCommand[1] = "-Xmx%sM" % config[3]
    server = AlphaServer(sub.Popen(startCommand,
                         stdout=sub.PIPE,
                         stderr=sub.PIPE,
                         stdin=sub.PIPE))

    while True:
        try:
            output = server.stderr
            if not output:
                print "\nNo output. Closing."
                time.sleep(1)
                raise SystemExit

            output = Output(output)

            DateTime = " ".join(output.DateTime)
            Type     = output.Type
            Message  = output.Message[0:]
            Player   = output.Player

            if not Player:
                print DateTime, Type, Message
            else:
                print DateTime, Type, Player, Message

        except Exception as error:
            print error
            server.say("FATAL ERROR: %s" % error)
            server.say("Stopping in 5 seconds.")
            time.sleep(5)
            server.stop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.