ts3服务器和客户端查询api
ts3quer的Python项目详细描述
从https://github.com/benediktschmitt/py-ts3派生,后者仅支持serverquery。然而,Benediktschmitts 版本更可能是最新的,因为它只为一个特定的项目派生,并且只实现一个小的 clientquery方法的子集。
此包为以下对象提供python 3 api
- TS3查询连接
- TS3查询事件
- TS3文件传输
您可以找到完整的api文档 here。
安装
此软件包已在PYPI上注册,因此您可以使用:
$ pip3 install ts3
TS3服务器配置
如果要向ts3服务器发送大量查询,请确保 TS3服务器的anti-flood protection未关闭连接。 因此,将运行ts3查询的主机添加到 query_ip_whitelist.txt您的ts3服务器:
$ echo"192.168.178.42" >> path/to/ts3/server/directory/query_ip_whitelist.txt
简介
了解这个库的最简单方法是查看 examples。
如果需要有关可能的查询命令的信息,请查看 ts3服务器查询手册。
示例
您可以在ts3.examples包中找到更多示例。
显示虚拟服务器上服务器ID为1的所有客户端:
#!/usr/bin/python3importts3withts3.query.TS3ServerConnection("localhost")asts3conn:# Note, that the client will wait for the response and raise a# **TS3QueryError** if the error id of the response is not 0.try:ts3conn.login(client_login_name="serveradmin",client_login_password="FoOBa9")exceptts3.query.TS3QueryErroraserr:print("Login failed:",err.resp.error["msg"])exit(1)ts3conn.use(sid=1)# Each query method will return a **TS3QueryResponse** instance,# with the response.resp=ts3conn.clientlist()print("Clients on the server:",resp.parsed)print("Error:",resp.error["id"],resp.error["msg"])# Note, the TS3Response class and therefore the TS3QueryResponse# class too, can work as a rudimentary container. So, these two# commands are equal:forclientinresp.parsed:print(client)forclientinresp:print(client)
向所有客户问好:
#!/usr/bin/python3importts3withts3.query.TS3ServerConnection("localhost")asts3conn:ts3conn.login(client_login_name="serveradmin",client_login_password="FoOBa9")ts3conn.use(sid=1)forclientints3conn.clientlist():msg="Hi {}".format(client["client_nickname"])ts3conn.clientpoke(clid=client["clid"],msg=msg)
向所有客户机打招呼,但将其与clientquery插件一起使用,而不是与serverquery连接一起使用:
#!/usr/bin/python3importts3withts3.query.TS3Connection("localhost",25639)asts3conn:ts3conn.auth(apikey="AAAA-BBBB-CCCC-DDDD-EEEEh")forclientints3conn.clientlist():msg="Hi {}".format(client["client_nickname"])ts3conn.clientpoke(clid=client["clid"],msg=msg)
事件处理(server query):
#!/usr/bin/python3importtimeimportts3withts3.query.TS3ServerConnection("localhost")asts3conn:ts3conn.login(client_login_name="serveradmin",client_login_password="FoOBa9")ts3conn.use(sid=1)# Register for eventsts3conn.servernotifyregister(event="server")whileTrue:event=ts3conn.wait_for_event()# Greet new clients.ifevent[0]["reasonid"]=="0":print("client connected")ts3conn.clientpoke(clid=event[0]["clid"],msg="Hello :)")
一个简单的TS3查看器:
#!/usr/bin/python3importts3# The examples package already contains this implementation.# Note, that the ts3.examples.viewer module has an helpful class to# build a complete channel tree of a virtual server: ChannelTreeNodefromts3.examples.viewerimportviewwithts3.query.TS3ServerConnection("localhost")asts3conn:ts3conn.login(client_login_name="serveradmin",client_login_password="FoOBa9")view(ts3conn,sid=1)
下载和上载文件:
#!/usr/bin/python3importts3withts3.query.TS3ServerConnection("localhost")asts3conn:ts3conn.login(client_login_name="serveradmin",client_login_password="FoOBa9")# Create a new TS3FileTransfer instance associated with the# TS3ServerConnection.ts3ft=ts3.filetransfer.TS3FileTransfer(ts3conn)# Upload the image *baz.png* to the channel with the id 2 on the# TS3 server.# Note the opening mode ("rb").withopen("baz.png","rb")asfile:ts3ft.init_upload(input_file=file,name="/baz.png",cid=2)# Download the file into *baz1.png*.withopen("baz1.png","wb")asfile:ts3ft.init_download(output_file=file,name="/baz.png",cid=2)
事件处理(client query):
#!/usr/bin/python3importtimeimportts3withts3.query.TS3ClientConnection("localhost")asts3conn:ts3conn.auth(apikey="AAAA-....-EEEE")ts3conn.use()# Register for eventsts3conn.clientnotifyregister(event="any")whileTrue:event=ts3conn.wait_for_event()print(event.parsed)
错误
如果发现错误,请报告或发送请求。
请同时报告语法或拼写错误。
版本控制
有关版本号,请查看http://semver.org/。