1

I am trying to make a connection from my javascript client in a browser to my python server and to send data from the server to the client, but when trying to connect I get the error: failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I tried using wss instead of ws. I also tried using socket.io instead of websocket, which wasn't helping either. I don't remember all the things I tried but none of them helped.

Javascript client:

var conn = new WebSocket('ws://localhost:2001');
    conn.onmessage = function (event) {
     console.log(event.data);

Python server:

# Echo server program
import socket

HOST = 'localhost'
PORT = 2001 
def sendSensorData(data):

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(HOST, PORT)
        s.listen(1)
        conn, addr = s.accept()
        with conn:
            print('Connected by', addr)
            while True:
                print(data)
                message = str(data).encode()
                try:
                # if not data: break
                    conn.sendall(message)
                except BaseException as e:
                    print (e)

Edit: This is the error from python:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/joep/Documents/GitHub/Walnut-installation-web-app/testAPI/workers/sensorStream.py", line 26, in sensorStream
    sendSensorData.sendSensorData(data)
  File "/Users/joep/Documents/GitHub/Walnut-installation-web-app/testAPI/functions/sendSensorData.py", line 19, in sendSensorData
    conn.send(message)
BrokenPipeError: [Errno 32] Broken pipe

Hopefully it is a simple trick to fix it.

8
  • Which Python version is this? Since 2.0, socket.bind() no longer accepts a pair of parameters, only a tuple; s.bind((HOST, PORT)) Commented Apr 26, 2019 at 8:06
  • I am using python3.7 Commented Apr 26, 2019 at 8:17
  • Then you should try to pass a tuple to s.bind() :) Also, note that there is a dedicated WebSockets package available for Python that wraps a high-level API around the low-level socket interface, implements the websockets protocol and provides asyncio-based concurrency out of the box. Commented Apr 26, 2019 at 8:33
  • I have seen that package, but the problem is that it is using asyncio, and I am running a few thread, and as far as I know, those two aren't that great at working at the same time. Commented Apr 26, 2019 at 8:54
  • I have edited the question with the error from python Commented Apr 26, 2019 at 8:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.