curl-and-python

Re: problem using pycurl for simple POST request

From: Geert Stappers <stappers_at_stappers.nl>
Date: Thu, 26 May 2005 08:05:34 +0200

On Wed, May 25, 2005 at 03:47:54PM -0400, Andrew Bushnell wrote:
> Andrew Bushnell wrote:
> >Andrew Bushnell wrote:
> >
> >>Hi,
> >>
> >>I am moving some code over to use pycurl (great library btw, thanks!!)
> >>but I am having difficulty replacing a section of code using pycurl.
> >>The old code/algorithm looks as something along the lines of:
> >>
> >>connection = HTTPConnection(host)
> >>connection.putheader("POST", "URLTOPOSTTO")
> >>connection.putheader("content-type", "text/xml")
> >>connection.putheader("Content-length", str(len(mydata)))
> >>connectino.putheader("mycustomheader", "mycustomvalue")
> >>connection.endheaders()
> >>connection.send(data)
> >>
> >>...
> >>
> >>then I get the reply and in this case, it is "data" to download/read
> >>off of the socket.
> >>
> >>I need/want to use pycurl to get round some proxy/ssl issues which it
> >>handles beautifully for me in other areas. However, I am stuck on 2
> >>questions/issues:
> >>
> >>#1: it seems to set the HTTPPOST, my data needs to be in the form of a
> >>list with field, data... This is not the case for me.
> >>
> >>#2: will a registered WRITEFUNCTION get the data requested via my post
> >>and custom header? Basically, the post contains an "action" to be
> >>peformed by the remote. When finished, it writes back the data,
> >>including content-type and length and based on that I read the data.
> >>
> >>Some curl psuedocode that I was working with:
> >>
> >>class Test:
> >> def myfunction():
> >> ...
> >>c = pycurl.Curl()
> >>t = Test()
> >>c.setopt(c.URL, 'url_posting_to')
> >># insecure for now..
> >>c.setopt(c.SSL_VERIFYPEER, False)
> >>c.setopt(c.SSL_VERIFYHOST, 1)
> >>c.setopt(c.PROXY, 'proxy')
> >># tunnel to get to secure site.
> >>c.setopt(c.HTTPPROXYTUNNEL, True)
> >>c.setopt(c.VERBOSE, 1)
> >># mydata is a python string object...
> >>c.setopt(c.HTTPPOST, mydata)
> >>c.setopt(c.WRITEFUNCTION, t.myfunction)
> >>c.perform()
> >>c.close()
> >>
> >>
> >>I will keep fishing/digging, but any help is appreciated.
> >>
>
> >Looks like part of my answer is to use the :
> >
> >curl.(c.POST, 1) and combined read function to get my string posted.
> >
> >
> >
> FYI, I got it all working. It was pretty simple and slick. Simply put,
> the keys were:
>
> #1: use of curl.POST with curl.READFUNCTION
> #2: use of curl.WRITEFUNCTION to get returned data.

The programmer in me says: I prefer the logical order.
Please reply below the text

Cheers
Geert Stappers

_______________________________________________
http://cool.haxx.se/mailman/listinfo/curl-and-python

Received on 2005-05-26