#!/usr/bin/env python3
import socket
import struct
import base64
import hashlib

# Connect to the WebSocket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.4.1', 80))

# Send HTTP WebSocket upgrade request
key = base64.b64encode(b'test-nonce').decode('utf-8')
request = f'''GET /ws HTTP/1.1\r
Host: 192.168.4.1\r
Upgrade: websocket\r
Connection: Upgrade\r
Sec-WebSocket-Key: {key}\r
Sec-WebSocket-Version: 13\r
\r
'''
s.send(request.encode())

# Read upgrade response
response = s.recv(4096)
print(response.decode())

# Send WebSocket TEXT frame with empty JSON '{}'
payload = b'{}'
mask = b'\x12\x34\x56\x78'
payload_masked = bytes(p ^ mask[i % 4] for i, p in enumerate(payload))

frame = struct.pack('BB', 0x81, 0x80 | len(payload))
frame += mask
frame += payload_masked

s.send(frame)
print("Sent empty JSON to WebSocket")
s.close()






