python - Django 和 C++ 通信
问题描述
我需要将一些数据从我的客户端机器中的 C++ 程序发送到 Django 服务器,以便使用 Python 处理数据并将其发送回另一台客户端机器。如果它是使用 json 的 ajax 和 javascript 之类的东西,那将很容易,但问题是,我研究了很多并找到了一个名为 Wt 的 C++ 库,它似乎有我需要的东西,但我不知道我会怎么做能够将数据发送到 Django 视图。我找不到任何特定于这个问题的有用代码,如果有人能告诉我如何做到这一点,我将不胜感激。
解决方案
wt是一个服务器库。你需要一个客户。您的 C++ 代码将充当浏览器并向您的 Django 服务器发出 HTTP 请求。有许多 C++ 库可以让您做到这一点。一个非常常见的是libcurl。如他们的示例POST
所示,使用 libcurl很容易:
#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;
}
推荐阅读
- java - 没有父 pom 故障安全集成测试的 SpringBoot 失败
- django - Django中基于行的权限
- html - 在菜单上突破其 div 容器的图像
- android - 如何在 android 中检查或匹配 InputDevice deviceid 和 UsbDevice deviceid?
- c# - 发布表单后如何获取JSON?
- fonts - GIMP/Fontconfig 在哪里存储 Windows 上的内置字体?
- sql - 查询以基于其他列返回具有重复值的行
- tar - 需要 tar 文件超过 48 小时
- scala - 如何在理解中取消(隐式)scalaz Monad?
- angular - 指令中组件的角度访问数据