Django 与 C++ 通信
Django and C++ communication
我需要将一些数据从客户端计算机中的 C++ 程序发送到 Django 服务器,以便使用 Python 处理数据并将其发送回另一台客户端计算机。如果它是 ajax 和 javascript 使用 json 之类的东西,那会很容易,但问题是,我研究了很多并找到了一个名为 Wt 的 C++ 库,它似乎有什么我需要,但我不知道如何将数据发送到 Django 视图。我找不到针对此问题的任何有用代码,如果有人能告诉我如何操作,我将不胜感激。
wt is a library for servers. You need a client. Your C++ code will act as the browser and make HTTP requests to your Django server. There are many C++ libraries that let you do that. A very common one is libcurl. It's easy to POST
with libcurl as shown by 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;
}
我需要将一些数据从客户端计算机中的 C++ 程序发送到 Django 服务器,以便使用 Python 处理数据并将其发送回另一台客户端计算机。如果它是 ajax 和 javascript 使用 json 之类的东西,那会很容易,但问题是,我研究了很多并找到了一个名为 Wt 的 C++ 库,它似乎有什么我需要,但我不知道如何将数据发送到 Django 视图。我找不到针对此问题的任何有用代码,如果有人能告诉我如何操作,我将不胜感激。
wt is a library for servers. You need a client. Your C++ code will act as the browser and make HTTP requests to your Django server. There are many C++ libraries that let you do that. A very common one is libcurl. It's easy to POST
with libcurl as shown by 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;
}