python - Node.js 作为 Django 的自定义(流式)上传处理程序

标签 python django javascript node.js uploading

我想使用 Django 构建一个以上传为中心的应用程序。一种方法是使用 nginx 的上传模块(非阻塞),但它有它的问题。 Node.js 应该是此类应用程序的理想选择。但是我怎样才能让 node.js 充当 upload_handler() for Django ?我不确定在哪里可以找到示例?

最佳答案

好吧,我不是该主题的专家或其他任何方面的专家,但我认为我理解它的方式是,nginx 是在为您的 django 应用程序提供服务的网络服务器前面运行的代理,对吗?

您可以构建一个简单的 node.js 服务器来执行相同的操作 - 监听端口 80,等待请求完全发送到服务器,然后将其转发到为 Django 应用程序提供服务的网络服务器。如果您的问题是网络服务器线程被长时间运行的上传耗尽,那么我认为这可以解决该问题。

这是一些代码 - 就在我的脑海中

var http = require('http');

var server = http.createServer(function (req, res) {
    var headers = req.headers;
    var url = req.url;
    var method = req.method;
    var body = '';
    req.addListener('data', function(chunk) {
        body += chunk;
    });
    req.addListener('end', function() {
        // at this point the request is completely uploaded (including files)
        // so it can be forwarded to the django webserver

        var dj_client = http.createClient(8000, 'localhost');
        var dj_req = dj_client.request(method, url, headers);
        dj_req.addListener('response', function (dj_res) {
            // here the response have been received from the django server
            // so we can return that to the waiting client
            res.writeHead(dj_res.status, dj_res.headers);

            dj_res.addListener('data', res.write);
            dj_res.addListener('end', res.close);
        });

        // send the request to the django webserver
        dj_req.write(body);
        dj_req.close();
    });
});

server.listen(80);

关于python - Node.js 作为 Django 的自定义(流式)上传处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2403280/

相关文章:

python - scons 顺序命令在依赖树中不符合预期

Python Windows 原始套接字无效绑定(bind)参数

JavaScript、HTML、JSP onClick 语句

javascript - 打开新窗口时可以删除父窗口吗?

python - 根据特定模式解析文件结构

python - 将 groupby 与多个索引列或索引一起使用时

python - 在python中序列化从mysqldb返回的结果集

javascript - 使用 Node(和 Django)的服务器端 javascript

python - django runserver 在 docker-compose up 中挂起,但在 docker-compose run 中正常运行

javascript - 从变量设置 cookie 值