0
我嘗試使用python-mpd2作爲項目。但是,大約一分鐘後連接就會中斷。代理python-mpd2在斷開連接的情況下重新連接
我用我低劣的Python技能,我嘗試做一個代理來解決這個問題。目標是重新連接,如果連接中斷。
這是據我得到
import mpd
class MPDProxy:
def __init__(self, host="localhost", port=6600, timeout=10):
self.client = MPDClient()
self.host = host
self.port = port
self.client.timeout = timeout
self.establish_connection(host, port)
def establish_connection(self, host, port):
self.client.connect(host, port)
def call(self, function, *args):
try:
return self.client.function(*args)
except mpd.ConnectionError:
self.establish_connection(self.host, self.port)
return self.client.function(*args)
然而,調用方法不起作用。
>>> client = MPDProxy()
>>> client.call(status)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'status' is not defined
據我瞭解此錯誤消息,它說:「我不知道有沒有所謂的‘地位’法」,我莫名其妙地理解。但是,我看不到如何解決這個問題(還)。