|
13 | 13 | print('Connected:', wlan.ifconfig()) |
14 | 14 |
|
15 | 15 | # Resolve hostname |
16 | | -host = 'echo.websocket.events' # Replace with your WSS server |
| 16 | +host = 'echo.websocket.events' |
17 | 17 | port = 443 |
18 | 18 | try: |
19 | 19 | addr_info = socket.getaddrinfo(host, port)[0][-1] |
|
63 | 63 | ssl_sock.close() |
64 | 64 | raise |
65 | 65 |
|
66 | | -# Read response in chunks with debugging |
| 66 | +# Read HTTP response until \r\n\r\n |
67 | 67 | response_bytes = bytearray() |
68 | 68 | max_read = 1024 # Maximum bytes to read |
69 | | -read_timeout = 10 # Timeout in seconds (adjust as needed) |
| 69 | +read_timeout = 2 # Reduced timeout (seconds) |
70 | 70 | import time |
71 | 71 |
|
72 | 72 | start_time = time.time() |
73 | 73 | while len(response_bytes) < max_read: |
74 | 74 | try: |
75 | | - # Read a small chunk to avoid blocking too long |
76 | 75 | chunk = ssl_sock.read(128) |
77 | | - if not chunk: # EOF or connection closed |
| 76 | + if not chunk: |
78 | 77 | print('No more data received (EOF)') |
79 | 78 | break |
80 | 79 | print('Received chunk, length:', len(chunk), 'bytes:', chunk) |
81 | 80 | response_bytes.extend(chunk) |
82 | 81 | print('Total bytes received:', len(response_bytes)) |
| 82 | + # Check for end of HTTP headers (\r\n\r\n) |
| 83 | + if b'\r\n\r\n' in response_bytes: |
| 84 | + print('End of HTTP headers detected') |
| 85 | + break |
83 | 86 | except Exception as e: |
84 | 87 | print('Error reading chunk:', e) |
85 | 88 | break |
|
91 | 94 | print('Raw response bytes:', response_bytes) |
92 | 95 | print('Raw response hex:', response_bytes.hex()) |
93 | 96 |
|
94 | | -# Attempt to decode response |
| 97 | +# Split HTTP response from any subsequent data |
| 98 | +http_end = response_bytes.find(b'\r\n\r\n') + 4 |
| 99 | +if http_end < 4: |
| 100 | + ssl_sock.close() |
| 101 | + raise Exception('Invalid HTTP response: no headers found') |
| 102 | +http_response_bytes = response_bytes[:http_end] |
| 103 | +extra_bytes = response_bytes[http_end:] if http_end < len(response_bytes) else b'' |
| 104 | +print('HTTP response bytes:', http_response_bytes) |
| 105 | +print('Extra bytes (if any):', extra_bytes) |
| 106 | + |
| 107 | +# Decode HTTP response |
95 | 108 | try: |
96 | | - response = response_bytes.decode('utf-8') |
97 | | - print('Decoded response:', response) |
| 109 | + response = http_response_bytes.decode('utf-8') |
| 110 | + print('Decoded HTTP response:', response) |
98 | 111 | except UnicodeError as e: |
99 | 112 | 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')) |
| 113 | + # Dump printable characters as fallback |
| 114 | + printable = ''.join(c if 32 <= ord(c) < 127 else '.' for c in http_response_bytes.decode('latin-1')) |
111 | 115 | print('Printable characters:', printable) |
112 | 116 | ssl_sock.close() |
113 | | - raise Exception('Failed to decode response') |
| 117 | + raise Exception('Failed to decode HTTP response') |
114 | 118 |
|
115 | 119 | # Check for valid WebSocket handshake |
116 | 120 | if '101 Switching Protocols' not in response: |
117 | 121 | print('Handshake response:', response) |
118 | 122 | ssl_sock.close() |
119 | 123 | raise Exception('Handshake failed') |
120 | 124 |
|
| 125 | +# Push back extra bytes (WebSocket frame) to the socket |
| 126 | +if extra_bytes: |
| 127 | + print('Note: Extra bytes detected, will be handled by websocket module') |
| 128 | + |
121 | 129 | # Create WebSocket object |
122 | 130 | ws = websocket(ssl_sock, True) |
123 | 131 |
|
124 | 132 | # Send and receive data |
125 | 133 | ws.write('Hello, Secure WebSocket!') |
126 | | -data = ws.read(1024) |
127 | | -print('Received:', data) |
| 134 | +for _ in range(50): |
| 135 | + data = ws.read(1024) |
| 136 | + print('Received:', data) |
| 137 | + time.sleep_ms(100) |
128 | 138 |
|
129 | 139 | # Close connection |
130 | 140 | ws.close() |
0 commit comments