0

Running the server and client on mac with localhost everything works fine.

Running the python program (server) on the raspberry pi and try to access it using its url doesn't work.

Python Server:

class Strompreisgenerator:
    def __init__(self):
        self.ws = websockets.serve(self.echo, 'localhost', 5001)
        asyncio.get_event_loop().run_until_complete(self.ws)
        asyncio.get_event_loop().run_forever()

    async def echo(self, websocket, path):
        async for message in websocket:
            print(message)

Javascript Client:

var ws = new WebSocket("ws://www.tobiasschmocker.ch:5001");

While trying to instantiate the WebSocket the error "WebSocket network error: The operation couldn’t be completed. Connection refused" occurs in safari.

The Port 5001 is open on RPi. I also tried local IP. I forwarded the Port on my router but still nothing. If i trie other urls i get another error, so i suppose the url is correct but i have no rights somehow.

On my RPi i have ssh enabled, also php, apache, mysql and all the pip packages for my python server.

If you know, where the problem lies, i'd be happy to know. Thank you very much!

11
  • Can you please show how you use your Strompreisgenerator? Commented Feb 4, 2018 at 21:59
  • Can you please write what happens when you run nc -v www.tobiasschmocker.ch 5001 Commented Feb 4, 2018 at 22:00
  • @YonatanKiron with nc -v www.tobiasschmocker.ch 5001 i get: nc: connect to www.tobiasschmocker.ch port 5001 (tcp) failed: Connection refused Commented Feb 6, 2018 at 1:09
  • @RetoAebersold I create an instance of the Strompreisgenerator further down in the code. I run the py-File from the terminal: python3 path/Strompreisgenerator.py . There is also a MQTT-Clients publishing in the program. The MQTT-Broker runs on port 1883 on www.tobiasschmocker.ch and for now i publish on the topic "Sarah" using a cronjob. I hope this is not interfering with the websocket. MQTT works fine, but the websocket doesnt. I have this problem only if i run the program on the RPi - on my mac it works fine. I'll keep the program running if you want to try out the Websocket or MQTT. Commented Feb 6, 2018 at 1:23
  • @agittarius and when you run it from your RPi, using the Doman name, localhost, and 127.0.0.1. Commented Feb 6, 2018 at 6:48

1 Answer 1

5

Now the websocket is running and available via url from external!

Following is the solution for completeness. All I had to do is:

  • Update to Python Version 3.6. The websockets package seems to work only from Python Version 3.6. Here's the tutorial I used to install Python 3.6 on my RPi: https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f
  • use 0.0.0.0 instead of localhost as host within python. On sudo netstat -lptu in the RPi terminal, I could see that all the other ports (mqtt, ssh, etc) where set to 0.0.0.0 instead of localhost so I just tried and it works.

Having set all that, the websocket is now running. Thanks to @YonatanKiron & @Reto!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your message, effectively using 0.0.0.0 solved my problem.

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.