|
3 | 3 | import ubinascii |
4 | 4 | from websocket import websocket |
5 | 5 |
|
6 | | -# Connect to Wi-Fi |
| 6 | +# Connect to Wi-Fi (disabled as per your code) |
7 | 7 | if False: |
8 | 8 | wlan = network.WLAN(network.STA_IF) |
9 | 9 | wlan.active(True) |
10 | 10 | wlan.connect('your_ssid', 'your_password') |
11 | 11 | while not wlan.isconnected(): |
12 | 12 | pass |
13 | 13 | print('Connected:', wlan.ifconfig()) |
14 | | - |
| 14 | + |
15 | 15 | # Resolve hostname |
16 | 16 | host = 'echo.websocket.events' # Replace with your WSS server |
17 | 17 | port = 443 |
|
52 | 52 | 'Sec-WebSocket-Version: 13\r\n' |
53 | 53 | '\r\n' |
54 | 54 | ).format(host, key.decode()) |
55 | | -ssl_sock.write(handshake.encode()) |
56 | | -response = ssl_sock.read(1024).decode() |
| 55 | + |
| 56 | +# Send handshake request |
| 57 | +try: |
| 58 | + bytes_written = ssl_sock.write(handshake.encode()) |
| 59 | + print('Handshake sent, bytes written:', bytes_written) |
| 60 | + print('Handshake request:', handshake) |
| 61 | +except Exception as e: |
| 62 | + print('Failed to send handshake:', e) |
| 63 | + ssl_sock.close() |
| 64 | + raise |
| 65 | + |
| 66 | +# Read response in chunks with debugging |
| 67 | +response_bytes = bytearray() |
| 68 | +max_read = 1024 # Maximum bytes to read |
| 69 | +read_timeout = 10 # Timeout in seconds (adjust as needed) |
| 70 | +import time |
| 71 | + |
| 72 | +start_time = time.time() |
| 73 | +while len(response_bytes) < max_read: |
| 74 | + try: |
| 75 | + # Read a small chunk to avoid blocking too long |
| 76 | + chunk = ssl_sock.read(128) |
| 77 | + if not chunk: # EOF or connection closed |
| 78 | + print('No more data received (EOF)') |
| 79 | + break |
| 80 | + print('Received chunk, length:', len(chunk), 'bytes:', chunk) |
| 81 | + response_bytes.extend(chunk) |
| 82 | + print('Total bytes received:', len(response_bytes)) |
| 83 | + except Exception as e: |
| 84 | + print('Error reading chunk:', e) |
| 85 | + break |
| 86 | + if time.time() - start_time > read_timeout: |
| 87 | + print('Read timeout reached') |
| 88 | + break |
| 89 | + |
| 90 | +# Inspect raw bytes |
| 91 | +print('Raw response bytes:', response_bytes) |
| 92 | +print('Raw response hex:', response_bytes.hex()) |
| 93 | + |
| 94 | +# Attempt to decode response |
| 95 | +try: |
| 96 | + response = response_bytes.decode('utf-8') |
| 97 | + print('Decoded response:', response) |
| 98 | +except UnicodeError as e: |
| 99 | + print('UnicodeError during decode:', e) |
| 100 | + # Try decoding with 'ignore' to see partial response |
| 101 | + response = response_bytes.decode('utf-8', errors='ignore') |
| 102 | + print('Decoded with errors ignored:', response) |
| 103 | + # Try alternative encoding (e.g., latin-1) |
| 104 | + try: |
| 105 | + response = response_bytes.decode('latin-1') |
| 106 | + print('Decoded as latin-1:', response) |
| 107 | + except Exception as e: |
| 108 | + print('Latin-1 decode failed:', e) |
| 109 | + # Dump printable characters |
| 110 | + printable = ''.join(c if 32 <= ord(c) < 127 else '.' for c in response_bytes.decode('latin-1')) |
| 111 | + print('Printable characters:', printable) |
| 112 | + ssl_sock.close() |
| 113 | + raise Exception('Failed to decode response') |
| 114 | + |
| 115 | +# Check for valid WebSocket handshake |
57 | 116 | if '101 Switching Protocols' not in response: |
| 117 | + print('Handshake response:', response) |
58 | 118 | ssl_sock.close() |
59 | | - raise Exception('Handshake failed: ' + response) |
| 119 | + raise Exception('Handshake failed') |
60 | 120 |
|
61 | 121 | # Create WebSocket object |
62 | 122 | ws = websocket(ssl_sock, True) |
|
0 commit comments