Last active
January 22, 2021 16:19
-
-
Save kakopappa/d52228f03576495b0fee0896a5f5c462 to your computer and use it in GitHub Desktop.
Sinric python example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
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
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