Skip to main content

A teeworlds network protocol library, designed according to sans I/O (http://sans-io.readthedocs.io/) principles

THIS LIBRARY IS IN EARLY DEVELOPMENT

Do not get bamboozled by the mature looking readme!

This project is not in a very usable state yet. It is in very early development!

APIs might change and many essential features are missing!


install

pip install twnet_parser

sample usage

import twnet_parser.packet
packet = twnet_parser.packet.parse7(b'\x04\x0a\x00\xcf\x2e\xde\x1d\x04') # 0.7 close

print(packet) # => <class: 'TwPacket7'>: {'_version': '0.7', 'is_ddnet': False, 'payload_raw': b'\x04', 'payload_decompressed': b'\x04', 'header': <class: 'PacketHeader7'>, 'messages': [<class: 'CtrlClose'>]}
print(packet.header) # => <class: 'PacketHeader7'>: {'flags': ['control'], 'ack': 10, 'token': b'\xcf.\xde\x1d', 'num_chunks': 0}
for msg in packet.messages:
    print(msg.message_name) # => close

print(packet.to_json())
# {
#   "version": "0.7",
#   "payload_raw": "04",
#   "payload_decompressed": "04",
#   "header": {
#     "flags": [
#       "control"
#     ],
#     "ack": 10,
#     "token": "cf2ede1d",
#     "num_chunks": 0
#   },
#   "messages": [
#     {
#       "message_type": "control",
#       "message_name": "close",
#       "message_id": 4,
#       "reason": null
#     }
#   ]
# }

More examples can be found in the examples/ folder:

0.7

0.6 and 0.7

Teeworlds protocol support

Feature 0.6.4 0.6.5 0.7.0 - 0.7.5
Deserialize packet headers ✔️ ✔️
Deserialize chunk headers ✔️ ✔️ ✔️
Deserialize messages 90% 90% 90%
Deserialize snapshots
Deserialize connless packets ✔️ ✔️ ✔️
Serialize packet headers ✔️ ✔️ ✔️
Serialize chunk headers ✔️ ✔️ ✔️
Serialize messages 90% 90% 90%
Serialize snapshots
Serialize connless packets ✔️ ✔️

DDNet extensions protocol support

DDNet version Supported
19.3 and older ❌
19.5 ❌

Non-Features (also not planned for this library)

Feature Status Where to find it
Networking ❌ TODO: link if someone implemented it on top
Protocol version detection ❌ TODO: link if someone implemented it on top
Track sequence number state ❌ TODO: link if someone implemented it on top
Track connection state ❌ TODO: link if someone implemented it on top

Look elsewhere for these features. Or use this library to implement them on top.

This project is intentionally only covering parsing the protocol. Not fully implemeting a state machine of the protocol. Or a fully working client / server software.

If you want to build something with this library you do have to understand how the protocol works and when the client and server have to send what.

This protocol documentation should get you started to understand the basics.

Convenient defaults and fully customizable

from twnet_parser.packet import TwPacket7
from twnet_parser.messages7.game.cl_call_vote import MsgClCallVote

"""
The call to packet.pack() generates
a valid byte array that can be sent as an udp payload

It uses default values for things like:
 security token, acknowledge number, packet flags,
 chunk header (flags, size, seq),
 vote type, vote value, vote reason, vote force

It computes a valid chunk header size field based
on the payload length.

It sets the correct num chunks field in the packet header
based on the amount of messages you added (1 in this case)

While this has all fields set that packet would be dropped by a vanilla
implementation because the security token and sequence number is wrong.
So you have to take care of those your self.
"""
packet = TwPacket7()
msg = MsgClCallVote()
packet.messages.append(msg)
packet.pack() # => b'\x00\x00\x01\xff\xff\xff\xff\x00\x00\x80\x01default\x00default\x00default\x00\x00'



"""
Here we also send a Call vote message.
But this time we set a security token and a few other fields.

Note that we set num_chunks to 6 which is wrong because
we only send one message (MsgClCallVote).
But this library allows you to do so.
And it will not compute the correct amount.
But use your explicitly set wrong one instead.

This allows you to have full control and craft any kind of packet.
May it be correct or not.
"""
packet = TwPacket7()
packet.header.token = b'\x48\x1f\x93\xd7'
packet.header.num_chunks = 6
packet.header.ack = 638
packet.header.flags.control = False
packet.header.flags.compression = False
msg = MsgClCallVote()
msg.header.seq = 10
msg.type = 'option'
msg.value = 'test'
msg.reason = ''
msg.force = False
packet.messages.append(msg)
packet.pack() # => b'\x02~\x06H\x1f\x93\xd7\x00\x00\x80\x01option\x00test\x00\x00\x00'

Zero dependencies by default

Running pip install twnet_parser will not install any additional packages.

But there is an optional dependency for huffman compression. By default twnet_parser is using the huffman compression code from the huffman-py project which is written in pure python. If you have libtw2-huffman installed it will use that one instead. Because it is faster since it is written in rust and has better error handling. But since it is so much overhead it is not installed by default to keep twnet_parser light weight.

You can install it by running pip install libtw2-huffman or by running pip install -r requirements/optional.txt

You can also check which huffman backend is currently active with these lines of code

import twnet_parser.huffman
print(twnet_parser.huffman.backend_name()) # => rust-libtw2 or python-twnet_parser

development setup

git clone https://gitlab.com/teeworlds-network/twnet_parser
cd twnet_parser
python -m venv venv
source venv/bin/activate
pip install -r requirements/dev.txt
pre-commit install --hook-type commit-msg

tests and linting

# dev dependencies
pip install -r requirements/dev.txt

# run unit tests
pytest .

# run style linter
pylint src/

# run type checker
mypy src/

# or use the bundle script that runs all tests
./scripts/run_tests.sh

package and release

# manual
pip install -r requirements/dev.txt
version=0.14.2
sed -i "s/^__version__ =.*/__version__ = '$version'/" twnet_parser/__version__.py
python -m build
git tag -a "v$version" -m "# version $version"
python -m twine upload dist/*

# or use the interactive convience script
./scripts/release.sh

Metadata

Release files for twnet-parser 0.16.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for twnet-parser 0.16.1
File Size Uploaded Provenance Actions
twnet_parser-0.16.1.tar.gz 89.9 kB Without provenance Details for twnet_parser-0.16.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for twnet-parser 0.16.1
File Interpreter ABI Platform Actions
twnet_parser-0.16.1-py3-none-any.whl Python 3 none any Details for twnet_parser-0.16.1-py3-none-any.whl

Total release size: 364.3 kB

Release files / twnet_parser-0.16.1.tar.gz

Download URL twnet_parser-0.16.1.tar.gz
Size 89.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4d7a8225144b8002f5826d899a95e247dca2e864ec418974c39a80e65fe0738b
BLAKE2b-256 checksum
How to use checksums
b303367e96e0357c361823368b5dcac17b6ff885050422c94af1ad40a418d78b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.11

Release files / twnet_parser-0.16.1-py3-none-any.whl

Download URL twnet_parser-0.16.1-py3-none-any.whl
Size 274.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45176459244b00e5cee3f9e389d2170e15eb97debd48750df6cb37f53438445a
BLAKE2b-256 checksum
How to use checksums
730a15a046033a4b291b4e35a560fe0cdae6ed980c413ea9cbf462276e7aee0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.11

Security for twnet-parser 0.16.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.16.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page