2

我从未在 Python 中看到过这种情况,我很感兴趣是否有任何东西可以让您使用写入接口发送文件(例如 HTTP PUT 或 POST)?我只见过一个读取接口,您可以在其中传递文件名或file对象(urllib、请求等)

当然,我可能从来没有见过这个,我也很想知道。

4

1 回答 1

-1

虽然它看起来在高层次上是有意义的,但让我们尝试将文件接口映射到 HTTP 动词:

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

如您所见,文件接口与任何 RESTful 接口所需的 HTTP 动词集之间没有明确的映射关系。当然,您可能会一起破解仅使用GET(读取)和POST(写入)的实现,但这会破坏您需要扩展它以支持任何其他 HTTP 动词的第二次。

根据评论编辑:

我自己没有尝试过,但它似乎在深处(http/client.py),如果 data implements read,它会这样读取它:

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

请注意这样做可能会影响性能:

# 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).

所以是的,您应该能够将文件对象作为data参数传递。

于 2013-03-05T18:32:43.433 回答