День добрый, прошу помочь мне разобрать вот этот код.
Меня интересует формат составления url и формирование json.
Обьясните плиз что это- self._serialize_string(config)).
Спасибо
import base64
import json
import logging
import socket
import time
from . import exceptions
URL_FORMAT = “ws/{}:{}/api/v2/channels/samsung.remote.control?name={}”
class RemoteWebsocket():
“”“Object for remote control connection.”“”
def __init__(self, config):
import websocket
if not config:
config = 8001
if config == 0:
config = None
url = URL_FORMAT.format(config, config,
self._serialize_string(config))
self.connection = websocket.create_connection(url, config)
self._read_response()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def close(self):
“”“Close the connection.”“”
if self.connection:
self.connection.close()
self.connection = None
logging.debug(“Connection closed.”)
def control(self, key):
“”“Send a control command.”“”
if not self.connection:
raise exceptions.ConnectionClosed()
payload = json.dumps({
“method”: “ms.remote.control”,
“params”: {
“Cmd”: “Click”,
“DataOfCmd”: key,
“Option”: “false”,
“TypeOfRemote”: “SendRemoteKey”
}
})
logging.info(“Sending control command: %s”, key)
self.connection.send(payload)
time.sleep(self._key_interval)
_key_interval = 0.5
def _read_response(self):
response = self.connection.recv()
response = json.loads(response)
if response != “ms.channel.connect”:
self.close()
raise exceptions.UnhandledResponse(response)
logging.debug(“Access granted.”)
@staticmethod
def _serialize_string(string):
if isinstance(string, str):
string = str.encode(string)
return base64.b64encode(string).decode(“utf-8”)