I have this function in Python:

def create_packet(code, data):
    # Adding packet code and data length
    packet_code = struct.pack('!I', code)
    data_length = struct.pack('!I', len(data))
    # Full packet
    return b'1337' + packet_code + data_length + data

And i've tried to rewrite it to GDScript:

func create_packet(id: int, data: String) -> PackedByteArray:
	var packet: PackedByteArray
	var data1 = var_to_bytes(data)
	packet.append_array("1337".to_ascii_buffer())
	packet.append(id)
	packet.append(len(data1))
	
	packet.append_array(data1)
	return packet

But it not works. My server log and comparison between python client and godot:

[SERVER] Started at localhost:1337
[SERVER] Connection! ('127.0.0.1', 36038)
[SERVER] Created player PythonClient
[->PythonClient] ServerKeepAlive
From PythonClient: 10000
[PythonClient] ClientKeepAlive
From PythonClient: 1337'1
code and data length: 10005 1
[PythonClient] Received packet 10005
[PythonClient] ClientCreateRoom
[->PythonClient] Created room with id RNFB6
From PythonClient: 10000
[PythonClient] ClientKeepAlive
[SERVER] Connection! ('127.0.0.1', 45332)
[SERVER] Created player GodotClient
[->GodotClient] ServerKeepAlive
From GodotClient: 10000
[GodotClient] ClientKeepAlive
From GodotClient: 1337
                              1
code and data length: 353108992 256
[SERVER] Error with GodotClient | There is not enough data in the packet
[GodotClient] dissconected!

What i doing wrong?

    strnq yeah, no.

    struct.pack is a PYTHON SPECIFIC function that converts data into C structs. gdscript doesn't have anything like that (yet). there's some talk about implementing structs in gdscript.
    C structs are a PAIN IN THE ***, specially in python, because they don't just store a bunch of variables, they also have empty space used for padding. They work when converting from and to C structs between python and python or python and C apps, but that's it.

    use parsed strings or other data types instead. if you want to work with binary you'll have to make your own binary parser and stuff, which is a bunch of extra work (and can lead to core dumps very easily). I've done this, so I'll try to find the files and see if I can post it here for reference.

    edit: I found it, this is from a blender exporter I made:

    	array('i', textLength).tofile(file)
    	array('u', ob_name).tofile(file)#sequence of char divisible by 2
    	immutable_bytes = array('f', export_data)
    	immutable_bytes.tofile(file)
    	array('i', indices_array).tofile(file)

    Basically, I made my own custom file and stored binary data in it, then I wrote a binary parser in C++ that read the file. I would give the number of bytes to read before the data, and then read that amount of bytes of that type of data. The number of bytes to read and data type can be in front of the data, or in a header.

    edit2: are you trying to send the data from gdscript and read it on python? in that case you have to change the script that reads the data in python. a PackedByteArray is tightly packed, I think it's an array, you can't read it as a struct.