mirror of
https://github.com/nikdoof/python-ts3.git
synced 2025-12-16 19:42:22 +00:00
Switch to using context managers for locking.
This commit is contained in:
@@ -95,17 +95,16 @@ class TS3Proto():
|
|||||||
return self._logger
|
return self._logger
|
||||||
|
|
||||||
def connect(self, ip, port=10011, timeout=5):
|
def connect(self, ip, port=10011, timeout=5):
|
||||||
self.io_lock.acquire()
|
with self.io_lock:
|
||||||
try:
|
try:
|
||||||
self._telnet = telnetlib.Telnet(ip, port)
|
self._telnet = telnetlib.Telnet(ip, port)
|
||||||
except telnetlib.socket.error:
|
except telnetlib.socket.error:
|
||||||
raise ConnectionError(ip, port)
|
raise ConnectionError(ip, port)
|
||||||
|
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
self._connected = False
|
self._connected = False
|
||||||
|
|
||||||
data = self._telnet.read_until("\n\r", self._timeout)
|
data = self._telnet.read_until("\n\r", self._timeout)
|
||||||
self.io_lock.release()
|
|
||||||
|
|
||||||
if data.endswith("TS3\n\r"):
|
if data.endswith("TS3\n\r"):
|
||||||
self._connected = True
|
self._connected = True
|
||||||
@@ -126,12 +125,11 @@ class TS3Proto():
|
|||||||
commandstr = self.construct_command(command, keys=keys, opts=opts)
|
commandstr = self.construct_command(command, keys=keys, opts=opts)
|
||||||
self.logger.debug("send_command - %s" % commandstr)
|
self.logger.debug("send_command - %s" % commandstr)
|
||||||
|
|
||||||
self.io_lock.acquire()
|
with self.io_lock:
|
||||||
self._telnet.write("%s\n\r" % commandstr)
|
self._telnet.write("%s\n\r" % commandstr)
|
||||||
|
|
||||||
data = ""
|
data = ""
|
||||||
response = self._telnet.read_until("\n\r", self._timeout)
|
response = self._telnet.read_until("\n\r", self._timeout)
|
||||||
self.io_lock.release()
|
|
||||||
|
|
||||||
if not response.startswith("error"):
|
if not response.startswith("error"):
|
||||||
# what we just got was extra data
|
# what we just got was extra data
|
||||||
|
|||||||
Reference in New Issue
Block a user