LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-23-2009, 09:05 PM   #1
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Rep: Reputation: 16
[libcurl] disable output in terminal


I am doing some libcurl program in windows xp with VC 2005 (Win 32 program):

program reads a list of urls from input file and try to check http code for each of them.

The program runs pretty slow (10-15 seconds to fetch code per url !) ,and libcurl keep output everything into terminal when I run the program, what's more, it seems the program is NOT just fetching HTTP code in header but the whole page content.

The details code is as below:

Code:
   CURL *curl_handle;
	  //CURLcode res;

  	  curl_global_init(CURL_GLOBAL_ALL);

	  curl_handle = curl_easy_init();
	  curl_easy_setopt(curl_handle,CURLOPT_URL,url);

     
      curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
      curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS);

	  int curl_code = curl_easy_perform(curl_handle);

	  http_code = 0;

	  curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &http_code);
	 
	  curl_easy_cleanup(curl_handle);

	  curl_global_cleanup();
I been running this code inside BOINC, which disables libcurl terminal output and 10 times faster than this libcurl program alone....

Any idea ?


Thanks,

-Kun



Also could some one delete these two post, I posted it in software by mistake:

http://www.linuxquestions.org/questi...ntents-764069/

http://www.linuxquestions.org/questi...rminal-764041/

Thanks,
 
Old 10-24-2009, 11:44 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
If you do
Code:
curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);
libcurl will send a HEAD request so the page contents won't be served. You might gain even more speed if you use the multi interface which will let the program wait for all the urls at the same time.

Quote:
Originally Posted by Kenshung
The program runs pretty slow (10-15 seconds to fetch code per url !)
Hmm, I ran this on Google's home page and it took 0.148s with terminal output, page is pretty short though.

Quote:
Also could some one delete these two post, I posted it in software by mistake:
Use the report button on one of those posts (next to the quote button).
 
Old 10-24-2009, 11:47 PM   #3
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Original Poster
Rep: Reputation: 16
It is pretty quick but seems not work when coming to 'amazon.com' .

I got code '405' when putting CURLOPT_NOBODY in the program but actually it is OK (should be 200).

Is it possible I place it in a wrong place ?

My code is as below:




Code:
int http_code = -1;
if(http_code < 0)
	{
	
       CURL *curl_handle;
	  //CURLcode res;

  	  curl_global_init(CURL_GLOBAL_ALL);

	  curl_handle = curl_easy_init();
	  curl_easy_setopt(curl_handle,CURLOPT_URL,url);

     
      curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
      
	  curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1);  # is the position correct ?

	  int curl_code = curl_easy_perform(curl_handle);

	  http_code = 0;

	  curl_easy_getinfo(curl_handle, CURLINFO_HTTP_CODE, &http_code);
	 
	  curl_easy_cleanup(curl_handle);

	  curl_global_cleanup();
 
Old 10-25-2009, 09:24 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Looking at the headers
Code:
 curl_easy_setopt(curl_handle, CURLOPT_HEADER, 1);


HTTP/1.1 405 MethodNotAllowed 
Date: Sun, 25 Oct 2009 14:04:16 GMT
Server: Server
Set-Cookie: skin=noskin; path=/; domain=.amazon.com; expires=Sun, 25-Oct-2009 14:04:16 GMT
x-amz-id-1: 1632JFKY77DHQZRC5CR0
allow: POST, GET 
x-amz-id-2: G5819ZOStmRQaUwqwGExnyGBWPC/P4Ut
Vary: Accept-Encoding,User-Agent
Content-Type: text/html; charset=ISO-8859-1
Amazon won't respond to HEAD requests. I suppose you'll have to replace the WRITEFUNCTION to ignore output:
Code:
size_t ignore_output( void *ptr, size_t size, size_t nmemb, void *stream)
{
    (void) ptr;
    (void) stream;
    return size * nmemb;
}

...
    curl_handle = curl_easy_init();

    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &ignore_output);
...
 
Old 10-26-2009, 08:06 AM   #5
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Original Poster
Rep: Reputation: 16
Looks like a stone could not kill 2 birds this time...

I may pick the 'header request only' for the speed and universal enable sites.


Thanks a lot!


Quote:
Originally Posted by ntubski View Post
Looking at the headers
Code:
 curl_easy_setopt(curl_handle, CURLOPT_HEADER, 1);


HTTP/1.1 405 MethodNotAllowed 
Date: Sun, 25 Oct 2009 14:04:16 GMT
Server: Server
Set-Cookie: skin=noskin; path=/; domain=.amazon.com; expires=Sun, 25-Oct-2009 14:04:16 GMT
x-amz-id-1: 1632JFKY77DHQZRC5CR0
allow: POST, GET 
x-amz-id-2: G5819ZOStmRQaUwqwGExnyGBWPC/P4Ut
Vary: Accept-Encoding,User-Agent
Content-Type: text/html; charset=ISO-8859-1
Amazon won't respond to HEAD requests. I suppose you'll have to replace the WRITEFUNCTION to ignore output:
Code:
size_t ignore_output( void *ptr, size_t size, size_t nmemb, void *stream)
{
    (void) ptr;
    (void) stream;
    return size * nmemb;
}

...
    curl_handle = curl_easy_init();

    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &ignore_output);
...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[libcurl] disable output in terminal Kunsheng Linux - Software 1 10-28-2009 01:10 AM
How to disable bootloader output message wuke Linux - Embedded 2 04-29-2009 02:56 AM
Redirect terminal output to file AND terminal shan Linux - General 3 09-29-2006 08:36 AM
Disable errors output to console Tushar Pednekar Linux - Software 2 07-17-2006 08:10 PM
How to disable console output kamel Programming 2 06-02-2004 01:41 PM


All times are GMT -5. The time now is 02:43 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration