Download pool/shard009/Python/0080412_ws.py from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard009/Python/0080412_ws.py
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard009/Python/0080412_ws.py
-
curl -L -o 0080412_ws.py https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard009/Python/0080412_ws.py
2.81 kB
| import websocket | |
| import traceback | |
| import json | |
| import syslog | |
| import time | |
| import socket | |
| class Plugin(object): | |
| def __init__(self, queue): | |
| try: | |
| self.configuration_file = 'conf/{0}.conf'.format(self.__module__) | |
| self.websocket_url = None | |
| self.websocket = None | |
| self.wait_for_retry = 5 | |
| self.push_plugins_data_every = 3 | |
| self.queue = queue | |
| self.myhostname = self.__identify_myself() | |
| self.__load_configuration() | |
| self.__connect_to_server() | |
| except Exception as e: | |
| syslog.syslog( | |
| syslog.LOG_ERR, | |
| "Exception %s %r" % (e, traceback.format_exc(800)) | |
| ) | |
| def __load_configuration(self): | |
| with open(self.configuration_file) as f: | |
| self.configuration = json.loads(f.read()) | |
| #get self.websocket_url | |
| self.websocket_url = self.configuration['server']['websocket_url'] | |
| #get self.wait_for_retry | |
| self.wait_for_retry = self.configuration['server']['wait_for_retry'] | |
| #get self.push_plugins_data_every | |
| self.push_plugins_data_every = self.configuration['client']['push_plugins_data_every'] | |
| def __identify_myself(self): | |
| if socket.gethostname().find('.') >= 0: | |
| return socket.gethostname()[0:socket.gethostname().find('.')] | |
| else: | |
| return socket.gethostname() | |
| def __connect_to_server(self): | |
| while True: | |
| try: | |
| self.websocket = websocket.create_connection(self.websocket_url) | |
| break | |
| except Exception as e: | |
| time.sleep(self.wait_for_retry) | |
| def __get_queue_data(self): | |
| try: | |
| data = {} | |
| final_structure = {'host': self.myhostname, 'plugins': {}} | |
| while self.queue.qsize(): | |
| queue_data = self.queue.get(False) | |
| for key in queue_data: | |
| data[key] = queue_data[key] | |
| if data: | |
| final_structure['plugins'] = data | |
| return final_structure | |
| else: | |
| raise | |
| except: | |
| raise | |
| def __push_data(self): | |
| try: | |
| self.websocket.send(json.dumps(self.__get_queue_data())) | |
| except socket.error, e: | |
| if isinstance(e.args, tuple): | |
| self.__connect_to_server() | |
| except: | |
| pass | |
| def execute(self): | |
| while True: | |
| time.sleep(self.push_plugins_data_every) | |
| try: | |
| self.__push_data() | |
| except Exception as e: | |
| syslog.syslog( | |
| syslog.LOG_ERR, | |
| "Exception %s %r" % (e, traceback.format_exc(800)) | |
| ) | |