【问题标题】:Libcurl: can't connect to Django development serverLibcurl:无法连接到 Django 开发服务器
【发布时间】:2010-11-05 23:24:41
【问题描述】:

我的 C 应用程序需要连接到 Http 服务器并获得响应,而我在连接到不在端口 80 上运行的服务器时遇到了一些问题。

我在我的开发环境中使用端口 8000 上的 Django 开发服务器而不是 Apache。但是我只是在 Apache 的 80 端口上测试了连接到我的 Django 页面,并且 libcurl 能够连接。我可以在浏览器中连接到端口 8000,但 libcurl 不能。

我试过这个网址:http://192.168.1.186:8000/mypage/http://192.168.1.186/mypage/,然后将 CURLOPT_PORT 设置为 8000,但都没有成功。

具体错误是

libcurl error: 7
libcurl error: couldn't connect to server

服务器运行的是 CentOS 5.5,我确实通过 iptables 转发了端口 8000,并且 SELinux 被禁用。

我将只包含相关代码。

char* url = "http://192.168.1.186:8000/mypage/";
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, wait_for_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_perform(curl);

同样使用命令行 curl 也不起作用。
curl @987654321@

编辑:我发现如果我将 Apache 切换到 8000 端口 curl 工作正常,那么 Django 服务器中有一些东西忽略了我的请求。

【问题讨论】:

  • 你可以:telnet 192.168.1.186 8000 吗?如果这也失败了,那是网络/防火墙/配置问题,而不是编程/卷曲问题。
  • 连接被拒绝。我更新了这个问题,因为我发现当 Apache 在 8000 上时 curl 工作正常。所以 Django 服务器只是忽略了我的 curl 请求。

标签: c connection libcurl


【解决方案1】:

我明白了。

我正在使用这个命令来启动服务器
python manage.py runserver 192.168.1.186:8000

但由于某种原因,您必须改用 0.0.0.0:8000。
python manage.py runserver 0.0.0.0:8000

我不知道为什么会这样,但确实如此。

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 2017-10-11
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2021-08-23
    • 2010-11-02
    相关资源
    最近更新 更多