Libcurl:无法连接到 Django 开发服务器
我的 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 http://192.168.1.186:8000/mypage/
编辑:我发现如果我将 Apache 切换到端口 8000,curl 工作正常,因此 Django 服务器中的某些内容会忽略我的请求。
My C application needs to connect to an Http server and get a response, and I'm having some trouble connecting to a server that isn't running on port 80.
I'm using the Django development server on port 8000 instead of Apache in my development environment. But I did just test connecting to my Django page on Apache on port 80, and libcurl was able to connect. I can connect to port 8000 in my browser, but libcurl cannot.
I've tried this url: http://192.168.1.186:8000/mypage/
and http://192.168.1.186/mypage/
and then setting CURLOPT_PORT to 8000 but neither have worked.
The specific error is
libcurl error: 7
libcurl error: couldn't connect to server
The server is running CentOS 5.5, and I do have port 8000 forwarded through iptables and SELinux is disabled.
I'll just include the relevant code.
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);
Also using command line curl doesn't work either.curl http://192.168.1.186:8000/mypage/
Edit: I've discovered that if I switch Apache to port 8000 curl works fine, so there is something in the Django server that is ignoring my requests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我明白了。
我正在使用此命令来启动服务器
python manage.py runserver 192.168.1.186:8000
但由于某种原因你必须使用 0.0.0.0:8000 来代替。
python manage.py runserver 0.0.0.0:8000
我不知道为什么会这样,但确实如此。
I got it figured out.
I was using this command to start the server
python manage.py runserver 192.168.1.186:8000
But for some reason you have to use 0.0.0.0:8000 instead.
python manage.py runserver 0.0.0.0:8000
I don't know why that works, but it does.