使用Python websocket_server库时电视端WebSocket连接异常求助
Hey Mike, let's figure out why your TV browser can't establish a WebSocket connection while desktop and mobile browsers work fine. TV browsers often have quirky, non-standard WebSocket implementations, so the request differences you noticed are almost certainly the culprit. Here's a step-by-step plan to fix this:
1. Capture the full request from your TV browser
First, you need to see exactly what the TV is sending—right now you only have the desktop request. Modify your websocket_server code to log the complete raw request when a connection is attempted. For example, you can add logging in the connection handler:
from websocket_server import WebsocketServer def new_client(client, server): # Log the full raw request by accessing the underlying socket raw_request = client['handler'].request.recv(4096).decode('utf-8') print("TV Browser Request:\n", raw_request) print("------------------------") server = WebsocketServer(host='0.0.0.0', port=5001) server.set_fn_new_client(new_client) server.run_forever()
If that doesn’t capture the full request, use a tool like Wireshark to sniff the network traffic between the TV and your server—this will give you every detail of the handshake.
2. Compare the TV request against the standard WebSocket handshake
Once you have the TV's request, compare it to the RFC 6455 standard (the modern WebSocket protocol). Key things to check:
- Does it include the
Upgrade: websocketandConnection: Upgradeheaders? - Is there a
Sec-WebSocket-Keyheader (required for RFC 6455)? - Are the HTTP version and other headers formatted correctly?
Older TV browsers sometimes use outdated drafts like Hixie-76 or HyBi-00, which have different handshake requirements. The websocket_server library might only support the latest RFC 6455 standard, which would explain the failure.
3. Switch to a more compatible WebSocket library
If the TV is using an older protocol draft, websocket_server might not handle it. Try using the websockets library (note the plural)—it's actively maintained, fully compliant with RFC 6455, and has better handling of edge cases. Here's a simple example server:
import asyncio import websockets async def handle_connection(websocket): # Echo messages back to the client async for message in websocket: print(f"Received message: {message}") await websocket.send(f"Server received: {message}") async def main(): async with websockets.serve(handle_connection, "0.0.0.0", 5001): await asyncio.Future() # Keep the server running indefinitely asyncio.run(main())
Alternatively, you could use aiohttp's WebSocket support, which is also robust and widely used for production-grade servers.
4. Implement custom handshake logic (if needed)
If the TV uses a really old protocol that modern libraries don't support, you might need to manually handle the handshake. For example, Hixie-76 requires a different response calculation using the client's key. You can modify the server's connection handler to detect the protocol version and send the appropriate response.
Final Notes
TV browsers are often underpowered and lag behind in web standards, so compatibility can be tricky. Start by capturing the full request—once you know exactly what the TV is sending, you can tailor your server to handle it.
内容的提问来源于stack exchange,提问作者Mike Jones

