You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django架构选型咨询及WebSocket同时传输文本与字节数据的技术问题求助

Is Django the Right Choice for Your C++/Qt Client & Python AI Server Setup?

Hey there! Let's walk through your scenario and tackle each of your technical challenges one by one.

1. Should You Use Django for Your Server?

Django is absolutely a viable option here, but it depends on your priorities:

  • Pros of Django: Mature ecosystem, built-in admin interface, strong ORM, and excellent support for WebSockets via channels (which we'll dive into later). It's great if you need to add other features (like user auth, database persistence) down the line without reinventing the wheel.
  • Alternative to consider: If you want a lighter, async-first framework optimized for real-time communication, FastAPI might be a better fit. It has native WebSocket support, automatic async handling, and is generally faster for high-throughput real-time tasks. That said, Django + Channels can handle your use case perfectly well too.

Either way, moving away from PHP + shell_exec is a smart move—running Python AI code directly in your server will be more efficient, easier to debug, and give you better control over execution.

2. Fixing the StreamingHttpResponse Issue

Your experience with StreamingHttpResponse makes total sense: by default, Python (and web servers like Gunicorn) buffer output until a certain chunk size is reached, which is why you see all data at once. Even with small chunksize, you might run into partial data issues because streaming responses are unidirectional—you can't easily handle client feedback or confirmations mid-stream.

For real-time updates like your cell index pushes, WebSocket is definitely the right approach (you already made the correct call here). It's bidirectional, low-latency, and designed exactly for this kind of incremental, real-time communication.

3. Sending Text + Binary Data Over WebSocket

The key here is to structure your WebSocket messages so you can distinguish between text metadata and binary data. Here are two solid approaches:

Option 1: Encode Binary Data as Base64 in JSON

Wrap all your data in a JSON object, converting binary data to a Base64 string. This keeps everything as a text frame, which is easy to parse on both ends:

  • Server side (Python):
    import json
    import base64
    
    # Example: Sending cell indices (text) + a binary blob (e.g., partial Excel data)
    message = {
        "type": "cell_update",
        "cell_indices": [[0, 1], [2, 3]],
        "binary_data": base64.b64encode(binary_content).decode('utf-8')
    }
    await websocket.send_text(json.dumps(message))
    
  • Client side (Qt/C++):
    Parse the JSON string, extract the Base64-encoded binary data, then decode it back to bytes using Qt's built-in QJsonDocument and QByteArray::fromBase64() methods.

Option 2: Send Separate Frames with Context

Send a text frame first that describes the upcoming binary data, then send the binary frame. Your client can track the context to know what the binary data is for:

  • Server side:
    # First send a text frame with context
    await websocket.send_text(json.dumps({"type": "file_chunk", "chunk_id": 1}))
    # Then send the binary data
    await websocket.send_bytes(binary_chunk)
    
  • Client side:
    Listen for text frames to set context, then handle the next binary frame using that context. This is more efficient for large binary data since you avoid Base64 overhead.

4. Uploading Excel + Text Parameters Together

When your Qt client uploads the Excel file, use a multipart/form-data request to include both the file and your text parameters. Here's how to implement this:

Qt Client Code Snippet

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QUrl url("http://your-server/upload");
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

// Add text parameters
QHttpPart paramPart;
paramPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"user_id\""));
paramPart.setBody("12345");
multiPart->append(paramPart);

// Add Excel file
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"excel_file\"; filename=\"data.xlsx\""));
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
QFile *file = new QFile("path/to/your/excel.xlsx");
file->open(QIODevice::ReadOnly);
filePart.setBodyDevice(file);
file->setParent(multiPart); // Delete the file with the multiPart
multiPart->append(filePart);

QNetworkRequest request(url);
QNetworkReply *reply = manager->post(request, multiPart);
multiPart->setParent(reply); // Delete the multiPart with the reply

Django Server View

from django.http import JsonResponse

def upload_excel(request):
    if request.method == 'POST':
        # Get text parameters
        user_id = request.POST.get('user_id')
        # Get Excel file
        excel_file = request.FILES.get('excel_file')
        
        # Process the file and parameters here
        # ...
        
        return JsonResponse({"status": "success"})

Final Recommendation

For your use case, I'd lean towards either:

  • Django + Channels: If you want the robustness of Django and plan to expand the server with additional features later.
  • FastAPI + WebSockets: If you prioritize async performance and minimal setup for real-time communication.

Both will handle your Excel uploads, AI processing, and real-time cell update pushes smoothly once you implement the structured WebSocket messaging and multipart uploads as outlined above.

内容的提问来源于stack exchange,提问作者Dream59

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 22:07:43