Skip to content

Commit 6f3bc13

Browse files
ssl websocket debugging
1 parent ea89b41 commit 6f3bc13

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed

draft_code/modwebsocket_test.py

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import ubinascii
44
from websocket import websocket
55

6-
# Connect to Wi-Fi
6+
# Connect to Wi-Fi (disabled as per your code)
77
if False:
88
wlan = network.WLAN(network.STA_IF)
99
wlan.active(True)
1010
wlan.connect('your_ssid', 'your_password')
1111
while not wlan.isconnected():
1212
pass
1313
print('Connected:', wlan.ifconfig())
14-
14+
1515
# Resolve hostname
1616
host = 'echo.websocket.events' # Replace with your WSS server
1717
port = 443
@@ -52,11 +52,71 @@
5252
'Sec-WebSocket-Version: 13\r\n'
5353
'\r\n'
5454
).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
57116
if '101 Switching Protocols' not in response:
117+
print('Handshake response:', response)
58118
ssl_sock.close()
59-
raise Exception('Handshake failed: ' + response)
119+
raise Exception('Handshake failed')
60120

61121
# Create WebSocket object
62122
ws = websocket(ssl_sock, True)

0 commit comments

Comments
 (0)