Skip to content

Python Edition

Trollhunters501PC edited this page Mar 19, 2025 · 9 revisions

Pip

PIP must be installed in your project

pip Command:

pip install https://github.com/Creadores-Program/ServerWebGamePost/releases/download/1.2.1/ServerWebGamePost-1.2.0.Python.Edition.tar.gz

And:

pip install request
# or require in your script

in your File.py:

import ServerWebGamePost

Create Server

Caution

THE SERVER MUST BE OPENED IN A THREAD SEPARATE FROM THE MAIN ONE SINCE THE MODULE USED ENTERS AN INFINITE LOOP TO RECEIVE REQUESTS

Processing Datapackets

First you need to extend the ServerWebGamePost.Server.ProcessDatapackServer class

And to process the datapackets that the client sends you must implement processDatapack function Something like that:

class MyProcessorDatapacks(ServerWebGamePost.Server.ProcessDatapackServer):
    def processDatapack(self, datapack): # datapack = json object
        #code...

Send Datapackets

Simply in the ServerWebGamePost instance Execute the sendDataPacket function

Something like that:

# ServerWebGamePost.Server = server
server.sendDataPacket(identifier, json) #identifier you must obtain from the datapacket that the client gives in its datapacket (it is a String) json is datapacket in json object
# in ServerWebGamePost.Server.ProcessDatapackServer = self.server.serverFat

Ban IP

You can ban IPs with the banIp and unbanIp functions in ServerWebGamePost.Server class (The IP is String and is only that argument)

Filter Origins

If you never run filter origins the origin header will be "*" if you run it it will only accept the given origins (Origin Header) The functions addFilterOrigin and removeFilterOrigin in ServerWebGamePost.Server class only accept one string argument.

Create Instance

You must create a new ServerWebGamePost.Server instance

server = ServerWebGamePost.Server(port, imgSrc, processDatapacks)
# port = int(Here you must specify the port where the server opens), imgSrc = String (Directory in string of the server icon type image can be None), processDatapacks = ServerWebGamePost.Server.ProcessDatapackServer extended class(It should NOT be an instance)

Modify server

Get http.server.HTTPServer:

server.getHttpServer()

Stop Server

just run the stop function in the Server Class

Create Client

Processing Datapackets

You need to create a callback function.

Something like that:

def processDatapacks(datapack):
    # datapack = json

Send Datapackets

Simply in ServerWebGamePost.Client class Execute the sendDataPacket function

Something like that:

# client = ServerWebGamePost.Client
client.sendDataPacket(packet)
# packet = json
# Remember to also send the identifier key

Create Instance

You must create a new ServerWebGamePost.Client instance

client = ServerWebGamePost.Client(domain, port, isHttps)
# domain = String (server domain or IP)
# port = String (Server port)
# isHttps= boolean (whether the server has HTTPS encryption or not)
client.setProcessDatapacks(callback)
# callback = function(Callback in charge of handling data packets )