cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Problem using pycurl.WRITEFUNCTION

From: Kjetil Jacobsen <kjetilja_at_cs.uit.no>
Date: 29 Aug 2002 14:11:23 +0200

hello,

use the HEADERFUNCTION option to set a callback which is called when the
web server sends a reply.

note that 'connect() failed' is not a reply from the server. it's an
internal curl error which indicates that curl cannot connect to the
target web server. such errors are raised as exceptions by pycurl but
can be caught and transformed with regular string operations.

in addition, you can use getinfo() with HTTP_CODE as parameter to
extract the server response code (assuming that the server sends a
reply).

it's all shown in the code below:

--------------------------------------------------------

import sys, pycurl

## Callback function invoked when body data is ready
def body(buf):
    sys.stdout.write(buf)

## Callback function invoked when header data is ready
def header(buf):
    sys.stderr.write(buf)

c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://curl.haxx.se/')
c.setopt(pycurl.WRITEFUNCTION, body)
c.setopt(pycurl.HEADERFUNCTION, header)
try:
    c.perform()
except pycurl.error, msg:
    errno, text = msg
    sys.stderr.write('Error (error number %d): %s\n' % (text, errno))
sys.stderr.write('HTTP status code: %d\n' % c.getinfo(pycurl.HTTP_CODE))
c.close()

--------------------------------------------------------

hope this helps,

        - kjetil

On Thu, 2002-08-29 at 13:20, preetic wrote:
> hi all,
>
> In the application that i am developing once curl.perform() is executed,i
> want data be passed directly to me instead of it being passed to stdout.
> For that i am using curl.setopt(pycurl.WRITEFUNCTION,write_data).
> the write_data function is as follows:_
>
> def write_data(buffer):
> a=string.find(buffer,"Website has temporarily moved")
> return a
>
> I basically want to search the entire HTML for any error messages returned
> from the server.
> Am getting a pycurl error:-pycurl.Error7, 'connect() failed'
>
> I dunno how to solve this problem...
> Can anybody hepl me with this?
>
> Cheers
> Preeti
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf

-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
Received on 2002-08-29