【发布时间】:2013-01-21 12:40:15
【问题描述】:
我正在尝试创建一个 C++ 应用程序来登录 Django 服务器。我一直在寻找这个,但我还没有找到解决我的具体问题的方法。
我正在使用 libcurl 网站的示例之一来执行请求:
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1/");
/* 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);
}
return 0;
}
我找到了一个输入用户名和密码的选项:
curl_easy_setopt(curl, CURLOPT_USERPWD, "luis:123456");
但问题是我不知道 Django 是如何识别这个 curl 选项的。
其他可能性是完成 Django 提供的登录表单。我使用的表格是:
<html><body>
<form method="post" action="/login">
{% csrf_token %}
{% if form.errors %}
<p class="error">Login error</p>
{% endif %}
<table>
<tr>
<td>Username</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>Password</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
</form>
</body>
</html>
使用此选项,我的问题是我不知道如何使用 C++ 和 curl 获取和完成表单。
【问题讨论】:
-
您是否可以访问 django 代码,或者它对您来说是一个黑匣子?因为您可以创建一个新的 django 视图,该视图接受用户名和密码作为获取参数(可能还有您的 c++ 应用程序的某种自定义身份验证令牌),然后将用户登录并使用 curl 访问该视图。
-
是的,django 服务器是我的。我做了一个测试,发送我的用户并传入 url,视图已经实现了自定义身份验证。它有效,但不安全。
标签: c++ django authentication curl