-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathts3socket.py
83 lines (74 loc) · 2.49 KB
/
ts3socket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import socket
import select
import time
from myexception import MyException
# Buffer size
BUFFER_SIZE = 8192
# Reconnect
RECONNECT = False
class ts3socket:
def __init__(self, ip, port, sid, user, password):
self.ip = ip
self.port = port
self.sid = sid
self.user = user
self.password = password
self.connect()
def connect(self):
RECONNECT = False
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.ip, self.port))
try:
msg = self.receive()
if msg.startswith('TS3'):
if 'Welcome' not in msg:
self.receive()
self.send('use %s\n\r' % (self.sid, ))
if not self.receive().endswith('msg=ok\n\r'):
raise MyException("ServerID not found")
self.send('login ' + self.user + ' ' + self.password)
if not self.receive().endswith('msg=ok\n\r'):
raise MyException("username/password wrong")
else:
raise MyException("this isn't a TS3-Server, right?")
except (socket.error, socket.timeout):
self.disconnect()
def send(self, msg):
if RECONNECT is False:
try:
return self.sock.send((msg + '\n').encode())
except socket.error as conErr:
self.reconnect()
return False
def disconnect(self):
self.sock.close()
def receive(self):
if RECONNECT is False:
try:
return self.sock.recv(BUFFER_SIZE).decode()
except socket.error as conErr:
self.reconnect()
return False
def reconnect(self):
RECONNECT = True
print('reconnect...')
self.disconnect()
time.sleep(5)
self.connect()
def recv_all(self):
if RECONNECT is False:
try:
total_data = []
while True:
read_sockets, write_sockets, error_sockets = select.select([self.sock], [], [], 0.1)
if read_sockets:
data = self.sock.recv(BUFFER_SIZE).decode()
if not data:
break
total_data.append(data)
else:
break
return ''.join(total_data)
except socket.error as conErr:
self.reconnect()
return False