[Patches] urllib: POST with arbitrary mime type

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Mon, 24 Apr 2000 19:41:17 +0200


Guido wrote:
> One suggestion -- rather than testing for type(data)=3D=3Dtype(''),
> wouldn't it be better to test for isinstance(data, RequestBody)?

that would make it impossible to drop in an object implementing
the same protocol (gettype and getbody) -- and the old code
only worked for strings anyway...

but sure, I can change this.  how about:

    if isinstance(data, RequestBody):
        body =3D data
    else:
        body =3D RequestBody(str(data))

> Also, it seems that your Form class is broken.  It passes a dict as
> the body argument the the base class constructor, but it is later used
> as a string.

note that it also overrides the getbody() function.

but alright, I probably don't need to optimize for the case where
httplib.HOST fails to connect ;-)

btw, what about making the class even more flexible?  how about
something like:

    class RequestBody:
        ...
        def gettype(self):
            return "application/x-www-form-urlencoded"
        def getrequest(self):
            return "POST"
        def sendheader(self, http):
            http.putrequest(self.getrequest())
            http.putheader("Content-type", self.gettype())
            http.putheader("Content-length", str(self.bytes))
        def sendbody(self, http) -> send
            http.send(self.data)

> I suggest that you test this one more extensively.  Perhaps add some
> testcases to test_urllib.py?

gotta create that one first, right?

</F>