Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Last active January 22, 2021 16:19
Show Gist options
  • Save kakopappa/d52228f03576495b0fee0896a5f5c462 to your computer and use it in GitHub Desktop.
Save kakopappa/d52228f03576495b0fee0896a5f5c462 to your computer and use it in GitHub Desktop.
Sinric python example
#!/usr/bin/python
import websocket
import thread
import time
import base64
def on_message(ws, message):
print message
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
# Attemp to reconnect with 2 seconds interval
time.sleep(2)
initiate()
def on_open(ws):
print "### Initiating new websocket connection ###"
def initiate():
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://iot.sinric.com",
header={'Authorization:' + base64.b64encode('apikey:[replace with your api key]')},
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
if __name__ == "__main__":
initiate()
@krulkip
Copy link

krulkip commented Apr 27, 2019

Having a problem on this using python3.5.
'TypeError: a bytes-like object is required, not 'str'
It is related to the base64.b64encode command

@luis-gustta
Copy link

Having a problem on this using python3.5.
'TypeError: a bytes-like object is required, not 'str'
It is related to the base64.b64encode command

You should use Python2, or manually convert the string to base64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment