简体   繁体   中英

Libcurl: can't connect to Django development server

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM