【问题标题】:Django and C++ communicationDjango 和 C++ 通信
【发布时间】:2019-04-10 11:51:36
【问题描述】:

我需要将一些数据从客户端机器中的 C++ 程序发送到 Django 服务器,以便使用 Python 处理数据并将其发送回另一台客户端机器。如果它是使用 json 的 ajax 和 javascript 之类的东西,那将很容易,但问题是,我研究了很多并找到了一个名为 Wt 的 C++ 库,它似乎有我需要的东西,但我不知道我会怎么做能够将数据发送到 Django 视图。我找不到任何针对此问题的有用代码,如果有人能告诉我如何做到这一点,我将不胜感激。

【问题讨论】:

    标签: python c++ django wt


    【解决方案1】:

    wt 是一个服务器库。你需要一个客户。您的 C++ 代码将充当浏览器并向您的 Django 服务器发出 HTTP 请求。有许多 C++ 库可以让您做到这一点。一个很常见的是libcurlPOST 使用 libcurl 很容易,如 their example 所示:

    #include <stdio.h>
    #include <curl/curl.h>
    
    int main(void)
    {
      CURL *curl;
      CURLcode res;
    
      /* In windows, this will init the winsock stuff */ 
      curl_global_init(CURL_GLOBAL_ALL);
    
      /* get a curl handle */ 
      curl = curl_easy_init();
      if(curl) {
        /* First set the URL that is about to receive our POST. This URL can
           just as well be a https:// URL if that is what should receive the
           data. */ 
        curl_easy_setopt(curl, CURLOPT_URL, "http://my.django.server/some/url");
        /* Now specify the POST data */ 
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
    
        /* Perform the request, res will get the return code */ 
        res = curl_easy_perform(curl);
        /* Check for errors */ 
        if(res != CURLE_OK)
          fprintf(stderr, "curl_easy_perform() failed: %s\n",
                  curl_easy_strerror(res));
    
        /* always cleanup */ 
        curl_easy_cleanup(curl);
      }
      curl_global_cleanup();
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多