# $Id: curl.py,v 1.3 2002/07/09 15:31:54 kjetilja Exp $

## System modules
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO
import urllib, mimetools

## PycURL module
import pycurl


class Curl:

    def __init__(self, url, filename=None, data=None):
        self.h = []
        self.status = None
        self.server_reply = StringIO()
        self.c = pycurl.init()
        self.url = url
        self.data = data
        self.c.setopt(pycurl.URL, self.url)
        self.c.setopt(pycurl.HEADERFUNCTION, self.server_reply.write)

        if filename == None:
            self.fp = StringIO()
            self.c.setopt(pycurl.WRITEFUNCTION, self.fp.write)
        else:
            self.fp = open(filename, 'w')
            self.c.setopt(pycurl.WRITEDATA, self.fp)
        if self.data != None:
            self.c.setopt(pycurl.POST, 1)
            self.c.setopt(pycurl.POSTFIELDS, urllib.urlencode(self.data))

    def set_url(self, url):
        self.c.setopt(pycurl.URL, url)

    def add_header(self, *args):
        self.h.append(args[0] + ': ' +args[1])

    def retrieve(self):
        if self.h != []:
            self.c.setopt(pycurl.HTTPHEADER, self.h)
        self.c.perform()
        self.status = self.c.getinfo(pycurl.HTTP_CODE)
        return self.status

    def info(self):
        self.server_reply.seek(0,0)
        self.server_reply.readline() # discard http return code
        return mimetools.Message(self.server_reply)

    def close(self):
        self.c.cleanup()
        self.server_reply.close()
        self.fp.close()

    def __del__(self):
        self.close()


if __name__ == "__main__":
    c = Curl('http://curl.haxx.se/')
    c.retrieve()
    c.close()

@@ -12,17 +12,17 @@
 .py,v 1.
-3
+4
  2002/07
@@ -33,12 +33,12 @@
 15:3
-1:54
+8:10
  kje
@@ -266,20 +266,16 @@
 rl, file
-name
 =None, d
@@ -580,20 +580,16 @@
  if file
-name
  == None
@@ -726,27 +726,12 @@
 p = 
-open(filename, 'w')
+file
 %0A   

use file objects instead of file names to the constructor to allow writing to things like sys.stdout or sockets.