PyTS3

repository·master·Indexed 41 days ago

https://github.com/benediktschmitt/py-ts3

A Python 3.2+ API for interacting with TeamSpeak 3 (TS3) servers. It supports query connections, event handling, and file transfers. The library provides the TS3Connection class for authentication and query management, and the TS3FileTransfer class for uploading and downloading files. While v1 is in maintenance mode, v2 is recommended for new projects as it adds support for SSH and the TS3 client query interface.

Tokens
2.2K
Snippets
12
Records
14
Agent score
51%

What's inside PyTS3

  1. What is PyTS3?

    master

    PyTS3 is a Python 3.2+ package designed to interface with TeamSpeak 3 (TS3) servers. It provides a programmatic API for the following core functionalities:

    • TS3 query connections: Establishing and managing connections to the TS3 server query interface.
    • TS3 query events: Handling and responding to server-side events.
    • TS3 file transfers: Managing file operations via the TS3 protocol.
  2. Handle TS3 query responses

    master

    Every query method returns a ts3.query.TS3QueryResponse instance.

    • Use .parsed to access the parsed data.
    • Use .error["id"] and .error["msg"] to inspect error details.
    • The TS3QueryResponse class (and its parent TS3Response) acts as a container; you can iterate over the response object directly to loop through parsed items.
    # Example of accessing parsed data and iterating
    resp = ts3conn.clientlist()
    print("Clients on the server:", resp.parsed)
    print("Error:", resp.error["id"], resp.error["msg"])
    
    # These two loops are equivalent:
    for client in resp.parsed:
        print(client)
    for client in resp:
        print(client)
  3. Avoid TS3 anti-flood protection

    master

    If your application sends a high volume of queries, the TS3 server's anti-flood protection may close your connection. To prevent this, add your host's IP address to the query_ip_whitelist.txt file in your TS3 server directory.

    $ echo "192.168.178.42" >> path/to/ts3/server/directory/query_ip_whitelist.txt
  4. Troubleshoot unexpected disconnects due to anti-flood settings

    master

    If your Python script is being disconnected unexpectedly, the TeamSpeak 3 server might be triggering its anti-flood protection. By default, the server limits the number of queries a host can send per minute.

    To resolve this, you can either increase the query limit in your TS3 server configuration or add the IP address of the machine running your script to the server's query whitelist by appending it to query_ip_whitelist.txt.

    $ # In your TS3 server folder:
    $ echo "192.168.178.42" >> query_ip_whitelist.txt
  5. Prevent disconnects due to max-idle-time

    master

    TeamSpeak 3 servers automatically close idle connections after 10 minutes. To prevent this, you must send a keepalive signal to the server. Use the send_keepalive method of the TS3BaseConnection class to send an empty query. Ensure this method is called at least once every 10 minutes to maintain the connection.

    # Use the send_keepalive method on your connection instance
    connection.send_keepalive()
  6. Example: Implement a basic TeamSpeak 3 bot with hello_bot.py

    master

    The hello_bot.py example demonstrates how to create a basic TeamSpeak 3 bot using the py-ts3 library. This script typically connects to a TS3 server, handles basic events, and responds to user interactions. You can use this as a template for building more complex automation or bot features.

    # Note: The actual implementation is located in the source file:
    # ../../ts3/examples/hello_bot.py
  7. Use the viewer.py example script

    master

    The viewer.py script is an example implementation provided in the repository to demonstrate how to use the PyTS3 library. You can download or locate it in the ts3/examples/ directory to use as a reference for building your own TS3 client applications.

    # The actual implementation is located at ts3/examples/viewer.py
  8. Connect to a TS3 server and handle errors

    master

    Use ts3.query.TS3Connection as a context manager to establish a connection. Use .login() to authenticate.

    Error Handling: The client waits for responses and will raise a ts3.query.TS3QueryError if the response error ID is not 0. You can access the error details via err.resp.error["msg"].

    import ts3
    
    with ts3.query.TS3Connection("localhost") as ts3conn:
        try:
            ts3conn.login(
                client_login_name="serveradmin",
                client_login_password="FoOBa9"
            )
        except ts3.query.TS3QueryError as err:
            print("Login failed:", err.resp.error["msg"])
            exit(1)
    
        ts3conn.use(sid=1)