Create Socket for automation

Hello

I created a small Python3 script to act as a brigde between an automation system and the volumio player,

Like that i can load a playlist of webradios and select 1 of the webradio stations in that playlist by sending a "stream0-stream1-stream2-stream3 comamnds toward a created port on the volumio.

I have tested it on a python windows 10 and it is working, however if i try to run it on the volumio python (updated to python3) i have the next error.

1/ print(f"Connection from {address} has been established.") -> give me an error
2/ print(“Listen…”) -> after that line the program did create the socket -

Any solutions?

import socket
import requests

http = ‘http://’
IPPlayer = ‘192.168.0.116’
cmdstring = ‘/api/v1/commands/?cmd=’

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 8888))
s.listen(1)

print(“Listen…”)

clientsocket, address = s.accept()
print(f"Connection from {address} has been established.")

clientsocket.send(bytes(“Hey there!!!”,“utf-8”))

while True:
msg = clientsocket.recv(7)
print(msg.decode(“utf-8”))

msg = msg.decode("utf-8")

if msg == "stream0":
    res = requests.get(http+IPPlayer+cmdstring+'clearQueue')
    res = requests.get(http+IPPlayer+cmdstring+'playplaylist&name=streamlist')
    print(res)

if msg == "stream1":
    res = requests.get(http+IPPlayer+cmdstring+'play&N=0')
    print(res)

if msg == "stream2":
    res = requests.get(http+IPPlayer+cmdstring+'play&N=1')
    print(res)

if msg == "stream3":
    res = requests.get(http+IPPlayer+cmdstring+'play&N=2')
    print(res)

Volumio uses socket.io, which is websocket in its own implementation. I suggest to use socket.io instead of generic websocket

hello

i installed the socketio on the raspberry with volumio

then i have the next error

volumio@volumio:~$ python3
Python 3.4.2 (default, Sep 26 2018, 07:16:01)
[GCC 4.9.2] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import socketio
sio = socketio.Client()

Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘module’ object has no attribute ‘Client’

No idea… This seems an error of the python module…

What about using nodejs?

You probably need to install pip install "python-socketio[client]"

Got it working with the socket+requests

It just need some cleaning up …

import socket
import requests
import sys
import time

print(“Starting program…wait 10sec…”)

time.sleep(10)

#stream0$0A -> load playlist + play track1
#stream1$0A -> jump to track1 + play
#stream2$0A -> jump to track2 + play
#stream3$0A -> jump to track3 + play
#stopall$0A -> stop playback any track
#exitsoc$0A -> stop program

http = ‘http://’
IPPlayer = ‘127.0.0.1:3000’
cmdstring = ‘/api/v1/commands/?cmd=’

HOST = ‘’
PORT = 1234

def my_exit():
print(“Exiting after error…”)
sys.exit()
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #needed to reopen asap the port
except socket.error as msg:
print ("Failed to create socket. Error code: " + str(msg[0]) + " , Error message : " + msg[1])
my_exit()
print(“Created Socket…”)

try:
s.bind((HOST,PORT))
except socket.timeout as msg:
print ("Failed to bind. Error code: " + str(msg[0]) + " , Error message : " + msg[1])
my_exit()
print(“Binded Socket…”)

s.listen(1)
print(“Listen…”)

while True:
clientsocket, address = s.accept()
print(“Received a connection…”+str(address))

    clientsocket.send(bytes("Hey there!!!","utf-8"))

while True:

    msg = clientsocket.recv(8)

    if len(msg) == 0:
            print("Connection closed...")
            break
    msg = msg.decode("utf-8")
    msg = msg[:-1]
    print(msg)
    if msg == "stream0":
            res = requests.get(http+IPPlayer+cmdstring+'clearQueue')
            print(res)
            res = requests.get(http+IPPlayer+cmdstring+'playplaylist&name=streamlist')
            print(res)
            print("Starting Playlist + Stream 1...")

    if msg == "stream1":
            res = requests.get(http+IPPlayer+cmdstring+'play&N=0')
            print(res)
            print("Starting Stream 1...")

    if msg == "stream2":
            res = requests.get(http+IPPlayer+cmdstring+'play&N=1')
            print(res)
            print("Starting Stream 2...")
      
    if msg == "stream3":
            res = requests.get(http+IPPlayer+cmdstring+'play&N=2')
            print(res)
            print("Starting Stream 3...")

    if msg == "stopall":
            res = requests.get(http+IPPlayer+cmdstring+'stop')
            print(res)
            print("Stopping any Stream ...")

    if msg == "exitsoc":
            s.close()
            break

s.close()