Skip to content Skip to sidebar Skip to footer

Upload A File With A File.write Interface

I've never seen this in Python and I'm interested if there is anything out there that will allow you to send files (for example HTTP PUT or POST) with a write interface? I've only

Solution 1:

While it might look like it would make sense at a high level, let's try to map the file interface to HTTP verbs:

file interface   http
------------------------
read             GET
                 HEAD
------------------------
write            POST
                 PUT
                 PATCH
------------------------
?                DELETE
                 OPTIONS

As you can see, there's no clear mapping between the file interface and the set of HTTP verbs that are required for any RESTful interface. Of course, you could likely hack an implementation together that only uses GET (read) and POST (write), but that will break the second you need to extend it to support any other HTTP verbs.

Edit based on comments:

I haven't tried it myself, but it seems like deep down (http/client.py), if data implements read, it will read it as such:

while1:
            datablock = data.read(blocksize)
            if not datablock:
                breakif encode:
                datablock = datablock.encode("iso-8859-1")
            self.sock.sendall(datablock)

Do note the possible performance hit in doing this though:

# If msg and message_body are sent in a single send() call,# it will avoid performance problems caused by the interaction# between delayed ack and the Nagle algorithm. However,# there is no performance gain if the message is larger# than MSS (and there is a memory penalty for the message# copy).

So yes, you should be able to pass in a file object as the data param.

Post a Comment for "Upload A File With A File.write Interface"