Python Forum
need to understand better __setup__.py, websocket-client, packaging
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need to understand better __setup__.py, websocket-client, packaging
#1
Any one familiar with websocket-client? https://github.com/websocket-client/websocket-client
I think my issue maybe more python syntax than any thing.
I was using ws=Websocket() and my 'import websocket'. This seemed to be working.
But the instillation was magic. I tried "pip install websocket" and it would error out. Then I did "pip install websocket-client". That seemed to work and I started debugging my code.
I am using PyCharm and it asked if I wanted to include the import into my __setup__.py I said yes and it quit.
[Edit]
Just saw this pip command and thought it might be relevant.
Output:
(venv) C:\Users\paul\PycharmProjects\pyalmondplus>pip list Package Version ---------------- ------- atomicwrites 1.1.5 attrs 18.1.0 click 6.7 colorama 0.3.9 more-itertools 4.2.0 pip 10.0.1 pluggy 0.6.0 py 1.5.4 pytest 3.6.3 pytest-runner 4.2 ruamel.yaml 0.15.42 setuptools 39.1.0 six 1.11.0 websocket-client 0.48.0 websockets 5.0.1 (venv) C:\Users\paul\PycharmProjects\pyalmondplus>
In the more info than you care, here is the code snippet.

# -*- coding: utf-8 -*-
import threading
import websocket from websocket-client
import json
import time


class PyAlmondPlus:

    def __init__(self, api_url, event_callback=None):
        self.api_url = api_url
        self.ws = None
        self.receive_task = None
        self.event_callback = event_callback
        self.keep_running = True
        self.client_running = False
        t = threading.Thread(target=self.api_dispatcher, args=())
        t.start()

    def __del__(self):
        print("Delete started")
        if self.ws is not None:
            self.stop()
        print("deleted")

    def connect(self):
        print("connecting")
        if self.ws is None:
            print("opening socket ("+self.api_url+')')
            self.ws = WebSocket()
            self.ws.connect(self.api_url)
            print("Socket connected")

    def disconnect(self):
        pass

    def get_device_list(self):
        my_message = '{"MobileInternalIndex":"231234","CommandType":"DeviceList"}'
        self.send(my_message)

    def send(self, message):
        print("sending "+message)
        self.ws.send(message)

    def receive(self):
        print("receive started")
        try:
            recv_data = self.ws.recv()
            print(recv_data)
        except Exception as e:
            print("Error")
            print("**************************\n"
                  + str(e) + "\n"
                  + "**************************")
            self.ws = None
            return
        print("receive ended")
        if self.keep_running:
            self.receive()

    def api_dispatcher(self):
        while self.keep_running:
            print("Dispatcher Start")
            if self.client_running:
                print("Client is running")
                if self.ws is None:
                    print("self.ws is none")
                    self.connect()
                    self.receive()

    def start(self):
        self.client_running = True

    def stop(self):
        print("Stop 1")
        if self.ws is not None:
            self.ws.close()
            self.ws = None
        self.keep_running = False
        print("Stop 2")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  websocket help johnbob 0 628 Jan-13-2024, 06:32 AM
Last Post: johnbob
  trying to stream data from a websocket to client tomtom 2 1,979 Jan-12-2023, 03:01 PM
Last Post: Vadanane
  Help with Websocket-Client Module - Infinite Loop CluelessChris 0 3,885 Apr-25-2021, 01:53 PM
Last Post: CluelessChris
  Websocket conection closes abnormally Joshua_Omolo 2 3,817 Feb-17-2021, 08:03 AM
Last Post: Joshua_Omolo
  Websocket server not async? korenron 0 1,732 Sep-23-2019, 01:40 PM
Last Post: korenron
  define a variable before looped websocket korenron 0 1,813 Sep-22-2019, 12:53 PM
Last Post: korenron
  Pro's and Con's of some different websocket libraries penright 0 2,336 Jun-29-2018, 12:07 PM
Last Post: penright

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020