Skip to content

Instantly share code, notes, and snippets.

@freyes
Last active August 4, 2023 16:09
Show Gist options
  • Save freyes/943e58cbf8e1f58ee757270c52f94f4c to your computer and use it in GitHub Desktop.
Save freyes/943e58cbf8e1f58ee757270c52f94f4c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# Usage example:
# python3 client.py "ws://127.0.0.1:6083/?token=5f7854b7-bf3a-41eb-857a-43fc33f0b1ec"
#
import sys
from ws4py.client.threadedclient import WebSocketClient
class LazyClient(WebSocketClient):
def run(self):
try:
while not self.terminated:
try:
b = self.sock.recv(4096)
sys.stdout.write(b)
sys.stdout.flush()
except: # socket error expected
pass
finally:
self.terminate()
if __name__ == '__main__':
if len(sys.argv) != 3 or not sys.argv[1].startswith("ws"):
print("Usage %s: Please use websocket url and origin")
print("Example: ws://127.0.0.1:6083/?token=xxx http://origin.example.com/")
exit(1)
try:
ws = LazyClient(sys.argv[1], protocols=['binary'],
headers=[('Origin', sys.argv[2])])
ws.connect()
while True:
# keyboard event...
c = sys.stdin.read(1)
if c:
ws.send(c)
ws.run_forever()
except KeyboardInterrupt:
ws.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment