def __init__(self, url, timeout=10): if not url.endswith('/'): url+='/' # POST or GET <url>/kernel # if there is a terms of service agreement, you need to # indicate acceptance in the data parameter below (see the API docs) reply = requests.post( url+'kernel', data={'accepted_tos':'true'}, headers={'Accept': 'application/json'}, ) # Subsequent connections (including websocket) must preserve # the cookies for the backend to route to the right server cookie = '' for key, value in reply.cookies.items(): cookie += '{0}={1}; '.format(key, value) # RESPONSE: {"id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the websocket channel url from that response = reply.json() self.kernel_url = response['ws_url']+'kernel/'+response['id']+'/' websocket.setdefaulttimeout(timeout) print self.kernel_url self._ws = websocket.create_connection(self.kernel_url+'channels', cookie=cookie) # initialize our list of messages self.shell_messages = [] self.iopub_messages = []
def __init__(self, url, timeout=10): if not url.endswith('/'): url += '/' # POST or GET <url>/kernel # if there is a terms of service agreement, you need to # indicate acceptance in the data parameter below (see the API docs) reply = requests.post( url + 'kernel', data={'accepted_tos': 'true'}, headers={'Accept': 'application/json'}, ) # Subsequent connections (including websocket) must preserve # the cookies for the backend to route to the right server cookie = '' for key, value in reply.cookies.items(): cookie += '{0}={1}; '.format(key, value) # RESPONSE: {"id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the iopub and shell websocket channel urls from that response = reply.json() self.kernel_url = response['ws_url'] + 'kernel/' + response['id'] + '/' websocket.setdefaulttimeout(timeout) print self.kernel_url self._shell = websocket.create_connection(self.kernel_url + 'shell', cookie=cookie) self._iopub = websocket.create_connection(self.kernel_url + 'iopub', cookie=cookie) # initialize our list of messages self.shell_messages = [] self.iopub_messages = []
def execute_request(self, code, store_history=False): # zero out our list of messages, in case this is not the first request if not self._running: self._create_new_session() # RESPONSE: {"id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the iopub and shell websocket channel urls from that websocket.setdefaulttimeout(self.timeout) self._ws = websocket.create_connection(self.kernel_url + 'channels', header={'Jupyter-Kernel-ID': self.kernel_id}) self._send_first_message() self._running = True code = self._code_keepalive(code) self.shell_messages = [] self.iopub_messages = [] # Send the JSON execute_request message string down the shell channel msg = self._make_execute_request(code, store_history) self._ws.send(msg) self._get_messages() return {'kernel_url': self.kernel_url, 'shell': self.shell_messages, 'iopub': self.iopub_messages}
def _client_remote_thread(self): if self._ws_remote: return is_ssl = self._is_ssl_connection() url = self._format_websocket_url(self._WS_ENDPOINT_REMOTE_CONTROL, is_ssl=is_ssl) sslopt = {"cert_reqs": ssl.CERT_NONE} if is_ssl else {} websocket.setdefaulttimeout(self.timeout) self._ws_remote = websocket.WebSocketApp( url, on_message=self._on_message_remote, on_ping=self._on_ping_remote, ) _LOGGING.debug("Thread SamsungRemote started") # we set ping interval (1 hour) only to enable multi-threading mode # on socket. TV do not answer to ping but send ping to client self._ws_remote.run_forever(sslopt=sslopt, ping_interval=3600) self._is_connected = False if self._ws_art: self._ws_art.close() if self._ws_control: self._ws_control.close() self._ws_remote.close() self._ws_remote = None _LOGGING.debug("Thread SamsungRemote terminated")
def create(self): while self.active: try: if self.aw.seriallogflag: self.aw.addserial("wsport create()") websocket.setdefaulttimeout(self.connect_timeout) #websocket.enableTrace(True) self.ws = websocket.WebSocketApp("ws://{}:{}/{}".format(self.host,self.port,self.path), on_message=self.onMessage, on_error=self.onError, on_ping=self.onPing, on_pong=self.onPong, on_close=self.onClose, on_open=self.onOpen ) self.ws.run_forever( skip_utf8_validation=True, ping_interval=self.ping_interval, ping_timeout=self.ping_timeout) except Exception as e: self.aw.qmc.adderror(QApplication.translate("Error Message","WebSocket connection failed: {}",None).format(e)) if self.aw.seriallogflag: self.aw.addserial("wsport create() error: {}".format(e)) time.sleep(self.reconnect_interval) if self.active: self.aw.sendmessage(QApplication.translate("Error Message","Reconnecting WebSocket",None)) self.ws = None
def opensong_connect(self): # websocket.enableTrace(True) websocket.setdefaulttimeout(10.0) url = "ws://%s:%d/ws" % (self.config.host, self.config.port) self.websocket = websocket.WebSocketApp(url, on_open=osws_on_open, on_data=osws_on_data, on_error=osws_on_error, on_close=osws_on_close)
def start(self): self.ws = websocket.WebSocketApp("ws://{0}/".format(self.obsurl), on_message=self.on_message, on_error = self.on_error, on_open = self.on_open, header = ['Sec-WebSocket-Protocol: obsapi']) websocket.setdefaulttimeout(5) self.thread = Thread(target=self.ws.run_forever, name='thread-overlayswitcher.sceneswitcher.obsremote.ws.fun_forever') self.thread.start()
def __init__(self, url) -> None: self._url = url self._on_connect_handler = None self._on_failed_connect_handler = None self._on_message_handler = None self.connected = threading.Event() self.exit = threading.Event() self.retry = 0 websocket.setdefaulttimeout( _LOCAL_CONNECT_TIMEOUT_SEC if is_localhost_url(url) else _REMOTE_CONNECT_TIMEOUT_SEC)
def _connect(self): websocket.enableTrace(True) self._ws = websocket.WebSocketApp(url=self._url, on_open=self.on_open, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close) setdefaulttimeout(5) while True: print('try to connect...') self._ws.run_forever()
def __init__(self, api_handler: AbstractTokenApiHandler, api_name: str, query_params: Dict[str, str] = None, timeout: int = 5, auto_reconnect: bool = True, remote_exit_codes: List[int] = None): """ Create the websocket :param api_handler: Required: The api handler for authentication. :param api_name: The name of the ws api. :param query_params: URL Query parameters for this specific websocket. Use Dict[str,str] only here, i.e. set {"allscopes": "false"} instead of {"allscopes": False}. :param timeout: The timeout for websocket messages. Default is 5sec. :param auto_reconnect: Try to create a new websocket automatically when *self.send()* fails. If this is set to False, a WebSocketException will be raised instead. The default is True. :param remote_exit_codes: A list of close codes that can come in from the remote side. If one of the codes in this list matches the remote close code, *self.run_forever()* will exit instead of trying to reconnect. The default is None -> reconnect always. See https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1 """ if not api_handler: raise ValueError('Parameter api_handler= cannot be empty.') if not api_name: raise ValueError('Parameter api_name= cannot be empty.') self._url, self._protocol, self._proxy_hostname, self._proxy_port, self._proxy_auth = \ api_handler.get_websocket_config(api_name) if query_params: self._url += '?' + urlencode(query_params, safe="/,") self._api_handler = api_handler self._auto_reconnect = auto_reconnect self._reader_status = ReaderStatus.NONE self._reader_guard = threading.Condition() self._ws_guard = threading.RLock() self._backoff_condition = threading.Condition() if remote_exit_codes: self._remote_exit_codes = remote_exit_codes setdefaulttimeout(timeout) random.seed(time.time_ns())
def __init__(self, username: str, token: str, address: str, on_msg=None, ignore_ssl_cert=False): self.username = username self.token = token self.address = address self.on_msg = on_msg self.ws = None self.lock = Lock() self.reid = self.REID() self.running = False self.ignore_ssl_cert = ignore_ssl_cert setdefaulttimeout(60)
def execute(self): if 'timeout' in self.options: websocket.setdefaulttimeout(self.options['timeout']) self.ws = websocket.WebSocketApp(self.endpoint, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close) self.ws.on_open = self.on_open # deadmansnitch notify if 'snitch_url' in self.options: dms = DeadManSnitch(self.options['snitch_url']) dms.notify() print "### Starting... ###" self.ws.run_forever()
def __init__(self, url, timeout=10): if not url.endswith('/'): url+='/' # POST or GET <url>/kernel req = urllib2.Request(url=url+'kernel', data='',headers={'Accept': 'application/json'}) response = json.loads(urllib2.urlopen(req).read()) # RESPONSE: {"kernel_id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the iopub and shell websocket channel urls from that self.kernel_url = response['ws_url']+'kernel/'+response['kernel_id']+'/' websocket.setdefaulttimeout(timeout) self._shell = websocket.create_connection(self.kernel_url+'shell') self._iopub = websocket.create_connection(self.kernel_url+'iopub') # initialize our list of messages self.shell_messages = [] self.iopub_messages = []
def connect(self, tab=None, update_tabs=True): """Open a websocket connection to remote browser, determined by self.host and self.port. Each tab has it's own websocket endpoint - you specify which with the tab parameter, defaulting to 0. The parameter update_tabs, if True, will force a rescan of open tabs before connection. """ if update_tabs or not self.tablist: self.find_tabs() numtabs = len(self.tablist) if not tab: tab = numtabs - 1 wsurl = self.tablist[tab]['webSocketDebuggerUrl'] if self.soc and self.soc.connected: self.soc.close() websocket.setdefaulttimeout(3) self.soc = websocket.WebSocket() self.soc.settimeout(self.socket_timeout) self.soc.connect(wsurl) return self.soc
def __init__(self, url, timeout=10): if not url.endswith('/'): url += '/' # POST or GET <url>/kernel # if there is a terms of service agreement, you need to # indicate acceptance in the data parameter below (see the API docs) response = requests.post( url + 'kernel', data={'accepted_tos': 'true'}, headers={'Accept': 'application/json'}).json() # RESPONSE: {"id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the websocket channel url from that self.kernel_url = '{ws_url}kernel/{id}/'.format(**response) print(self.kernel_url) websocket.setdefaulttimeout(timeout) self._ws = websocket.create_connection( self.kernel_url + 'channels', header={'Jupyter-Kernel-ID': response['id']}) # initialize our list of messages self.shell_messages = [] self.iopub_messages = []
def __init__(self, url, timeout=10): if not url.endswith('/'): url+='/' # POST or GET <url>/kernel # if there is a terms of service agreement, you need to # indicate acceptance in the data parameter below (see the API docs) req = urllib2.Request(url=url+'kernel', data='accepted_tos=true',headers={'Accept': 'application/json'}) response = json.loads(urllib2.urlopen(req).read()) # RESPONSE: {"kernel_id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the iopub and shell websocket channel urls from that self.kernel_url = response['ws_url']+'kernel/'+response['kernel_id']+'/' websocket.setdefaulttimeout(timeout) print self.kernel_url self._shell = websocket.create_connection(self.kernel_url+'shell') self._iopub = websocket.create_connection(self.kernel_url+'iopub') # initialize our list of messages self.shell_messages = [] self.iopub_messages = []
def __init__(self, url, timeout=10): if not url.endswith('/'): url += '/' # POST or GET <url>/kernel # if there is a terms of service agreement, you need to # indicate acceptance in the data parameter below (see the API docs) response = requests.post( url + 'kernel', data={'accepted_tos': 'true'}, headers={'Accept': 'application/json'}).json() # RESPONSE: {"id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the websocket channel url from that self.kernel_url = '{ws_url}kernel/{id}/'.format(**response) print self.kernel_url websocket.setdefaulttimeout(timeout) self._ws = websocket.create_connection( self.kernel_url + 'channels', header={'Jupyter-Kernel-ID': response['id']}) # initialize our list of messages self.shell_messages = [] self.iopub_messages = []
def __init__(self, url, timeout=10): if not url.endswith('/'): url += '/' # POST or GET <url>/kernel req = urllib2.Request(url=url + 'kernel', data='', headers={'Accept': 'application/json'}) response = json.loads(urllib2.urlopen(req).read()) # RESPONSE: {"kernel_id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the iopub and shell websocket channel urls from that self.kernel_url = response['ws_url'] + 'kernel/' + response[ 'kernel_id'] + '/' websocket.setdefaulttimeout(timeout) self._shell = websocket.create_connection(self.kernel_url + 'shell') self._iopub = websocket.create_connection(self.kernel_url + 'iopub') # initialize our list of messages self.shell_messages = [] self.iopub_messages = []
def _client_art_thread(self): if self._ws_art: return is_ssl = self._is_ssl_connection() url = self._format_websocket_url(self._WS_ENDPOINT_ART, is_ssl=is_ssl, use_token=False) sslopt = {"cert_reqs": ssl.CERT_NONE} if is_ssl else {} websocket.setdefaulttimeout(self.timeout) self._ws_art = websocket.WebSocketApp( url, on_message=self._on_message_art, ) _LOGGING.debug("Thread SamsungArt started") # we set ping interval (1 hour) only to enable multi-threading mode # on socket. TV do not answer to ping but send ping to client self._ws_art.run_forever(sslopt=sslopt, ping_interval=3600) self._ws_art.close() self._ws_art = None _LOGGING.debug("Thread SamsungArt terminated")
def __init__(self, url, timeout=10): if not url.endswith('/'): url += '/' # POST or GET <url>/kernel # if there is a terms of service agreement, you need to # indicate acceptance in the data parameter below (see the API docs) req = urllib2.Request(url=url + 'kernel', data='accepted_tos=true', headers={'Accept': 'application/json'}) response = json.loads(urllib2.urlopen(req).read()) # RESPONSE: {"id": "ce20fada-f757-45e5-92fa-05e952dd9c87", "ws_url": "ws://localhost:8888/"} # construct the iopub and shell websocket channel urls from that self.kernel_url = response['ws_url'] + 'kernel/' + response['id'] + '/' websocket.setdefaulttimeout(timeout) print self.kernel_url self._shell = websocket.create_connection(self.kernel_url + 'shell') self._iopub = websocket.create_connection(self.kernel_url + 'iopub') # initialize our list of messages self.shell_messages = [] self.iopub_messages = []
def connect(self, timeout=DEFAULT_TIMEOUT): """Make a connection to the fetchcore server. :param timeout: The timeout (in seconds) for the connection. :raise fetchcore.exceptions.ConnectionError: Thrown if there is a problem creating communicating with fetchcore. """ if self.is_connected: # Don't connect twice return try: token = self.get_authentication_token() except exceptions.ConnectionError: return # The actual client that communicates with the server. websocket.setdefaulttimeout(timeout) self.__websocket = websocket.WebSocketApp(self.__url + '?token=' + token, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close, on_open=self.on_open) # The websocket library only allows static methods to be passed in for callbacks. We mask this by creating # an internal pointer to ourselves so we can call our WebSocketClient methods. self.__websocket.client = self # Wait for response and throw exception start = time.time() # TODO: handle auth when websockets want them thread = threading.Thread(target=self.wrapped_run_forever, kwargs={'ping_timeout': 10}) thread.start() while not self.is_connected and time.time() - start < timeout: time.sleep(0)
def testDefaultTimeout(self): self.assertEqual(ws.getdefaulttimeout(), None) ws.setdefaulttimeout(10) self.assertEqual(ws.getdefaulttimeout(), 10) ws.setdefaulttimeout(None)
from volttron.platform.messaging import topics from volttron.platform.messaging import headers as headers_mod import xml.etree.ElementTree as ET from gs_identities import * from gs_utilities import get_schedule, ForecastObject, Forecast from HistorianTools import publish_data import csv import pandas import math # from .FLAME import Baseline, LoadShift, LoadSelect, LoadReport, Status, format_timeperiod from .FLAME import * import websocket from websocket import create_connection websocket.setdefaulttimeout(60) import ssl utils.setup_logging() _log = logging.getLogger(__name__) __version__ = "0.1" # constants MINUTES_PER_HR = 60 MINUTES_PER_DAY = 24 * MINUTES_PER_HR MINUTES_PER_YR = 365 * MINUTES_PER_DAY FORCE_TIME = False # for debugging - lets user force a start time. max_load_report_length = 60
def _run(self, events): ppid = os.environ.get("AGENT_PARENT_PID") headers = [] if self._auth is not None: auth_header = 'Authorization: Basic ' + base64.b64encode( ('%s:%s' % self._auth).encode('latin1')).strip() headers.append(auth_header) subscribe_url = self._url.replace('http', 'ws') query_string = _events_query_string(events, self._agent_id) subscribe_url = subscribe_url + '?' + query_string try: drops = { 'drop_count': 0, 'ping_drop': 0, } self._start_children() def on_message(ws, message): line = message.strip() try: ping = '"ping' in line if len(line) > 0: # TODO Need a better approach here if ping: self._ping_queue.put(line, block=False) drops['ping_drop'] = 0 else: self._queue.put(line, block=False) except Full: log.info("Dropping request %s" % line) drops['drop_count'] += 1 drop_max = Config.max_dropped_requests() drop_type = 'overall' drop_test = drops['drop_count'] if ping: drops['ping_drop'] += 1 drop_type = 'ping' drop_test = drops['ping_drop'] drop_max = Config.max_dropped_ping() if drop_test > drop_max: log.error('Max of [%s] dropped [%s] requests exceeded', drop_max, drop_type) ws.close() if not _should_run(ppid): log.info("Parent process has died or stamp changed," " exiting") ws.close() def on_error(ws, error): raise Exception('Received websocket error: [%s]', error) def on_close(ws): log.info('Websocket connection closed.') def on_open(ws): log.info('Websocket connection opened') websocket.setdefaulttimeout(Config.event_read_timeout()) ws = websocket.WebSocketApp(subscribe_url, header=headers, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open) ws.run_forever() finally: for child in self._children: if hasattr(child, "terminate"): try: child.terminate() except: pass sys.exit(0)
from websocket import create_connection import ssl import json import pandas as pd import os import logging from random import randint import copy import ipdb # be sure to comment this out while running in Volttron instance from functools import reduce import pytz from gs_identities import * from gs_utilities import Forecast import numpy as np websocket.setdefaulttimeout(10) # set timeout quicker for testing purposes, normally 60 test_start_time = datetime.utcnow() _log = logging.getLogger(__name__) scale_factors = {"School": 1.0, "Canner": 0.2, "Mill": 0.1} USE_STATIC = False # Classes class IPKeys(object): """Parent class for request & response interactions with IPKeys""" def __init__(self, websocket):
def set_default_timeout(self, default_timeout): websocket.setdefaulttimeout(default_timeout) return self