更新 Debug/test_sse_connection.py

This commit is contained in:
2025-04-17 00:17:27 +08:00
parent c944ee970d
commit a6aa8ac8ce

View File

@@ -14,6 +14,7 @@ import http.client
import time import time
import sys import sys
import socket import socket
import json
def test_sse_connection(host="127.0.0.1", port=4550, path="/", timeout=30): def test_sse_connection(host="127.0.0.1", port=4550, path="/", timeout=30):
""" """
@@ -54,31 +55,24 @@ def test_sse_connection(host="127.0.0.1", port=4550, path="/", timeout=30):
start_time = time.time() start_time = time.time()
initial_data = "" initial_data = ""
received_complete_data = False received_complete_data = False
event_count = 0
max_events = 5 # Read 5 events
# Try to read data for up to 10 seconds # Try to read data for up to 10 seconds
while time.time() - start_time < 10 and not received_complete_data: while time.time() - start_time < 10 and not received_complete_data and event_count < max_events:
try: try:
# Read data in smaller chunks to avoid truncation # Read data in smaller chunks to avoid truncation
chunk = response.read(64).decode('utf-8') chunk = response.read(128).decode('utf-8')
if chunk: if chunk:
initial_data += chunk initial_data += chunk
print(f"Received chunk: {chunk}") print(f"Received chunk: {chunk}")
# Check if we've received complete JSON data # Count events
if '}' in chunk and 'data:' in initial_data: if "event:" in chunk:
# Continue reading to ensure we get complete events event_count += 1
for _ in range(10): # Try to read more chunks
try:
more_chunk = response.read(64).decode('utf-8')
if more_chunk:
initial_data += more_chunk
print(f"Received additional chunk: {more_chunk}")
else:
break
except:
break
time.sleep(0.1)
# Check if we've received complete JSON data
if '}' in chunk and 'data:' in initial_data and event_count >= max_events:
received_complete_data = True received_complete_data = True
break break
@@ -97,24 +91,49 @@ def test_sse_connection(host="127.0.0.1", port=4550, path="/", timeout=30):
print("-" * 50) print("-" * 50)
print(initial_data) print(initial_data)
print("-" * 50) print("-" * 50)
# Try to parse events
try:
events = []
current_event = {}
for line in initial_data.split('\n'):
line = line.strip()
if not line or line.startswith(':'):
continue
if line.startswith('event:'):
if current_event and 'event' in current_event:
events.append(current_event)
current_event = {}
current_event['event'] = line[6:].strip()
elif line.startswith('data:'):
if 'data' not in current_event:
current_event['data'] = line[5:].strip()
else:
current_event['data'] += line[5:].strip()
if current_event and 'event' in current_event:
events.append(current_event)
print("\nParsed Events:")
print("-" * 50)
for i, event in enumerate(events):
print(f"Event {i+1}: {event['event']}")
try:
data = json.loads(event['data'])
print(f"Data: {json.dumps(data, indent=2)}")
except:
print(f"Data: {event['data']}")
print()
print("-" * 50)
except Exception as e:
print(f"Error parsing events: {e}")
else: else:
print("Warning: No initial data received after 10 seconds") print("Warning: No initial data received after 10 seconds")
print("This doesn't necessarily mean the connection failed.") print("This doesn't necessarily mean the connection failed.")
print("The server might be configured to not send initial data.") print("The server might be configured to not send initial data.")
print("Connection is established (200 OK), which is a good sign.") print("Connection is established (200 OK), which is a good sign.")
# Read more data for a few seconds
print("\nReading data for 5 seconds...")
start_time = time.time()
while time.time() - start_time < 5:
try:
data = response.read(1024).decode('utf-8')
if data:
print(f"Received data: {data}")
except socket.timeout:
print("Socket timeout while reading additional data, continuing...")
time.sleep(0.5)
# Close connection # Close connection
print("\nClosing connection...") print("\nClosing connection...")
conn.close() conn.close()