Opened 7 years ago

Closed 6 years ago

#4017 enhancement closed fixed (fixed)

Add an IBodyProducer which produces bytes from a file-like object

Reported by: Jean-Paul Calderone Owned by: Jean-Paul Calderone
Priority: normal Milestone:
Component: web Keywords: httpclient IBodyProducer
Cc: Branch: branches/filebodyproducer-4017-2
branch-diff, diff-cov, branch-cov, buildbot
Author: exarkun

Description (last modified by Jean-Paul Calderone)

#886 introduces an HTTP client API along with a new interface, IBodyProducer, for providing request bodies in a streaming manner. This interface isn't quite the same as IProducer, so existing producers don't quite work with it.

For convenience, we should provide an implementation of this interface based on file-like objects (along the lines of twisted.protocols.basic.FileSender). This will be useful both for the obvious reason (sending files as request bodies), as well as as an example of how to implement these, and will also be usable with StringIO instances to send data constructed in memory (eg simple form posts).

Change History (13)

comment:1 Changed 7 years ago by ivank

Keywords: IBodyProducer added

comment:2 Changed 7 years ago by Jean-Paul Calderone

Keywords: httpclient added

comment:3 Changed 6 years ago by <automation>

Owner: Jean-Paul Calderone deleted

comment:4 Changed 6 years ago by Drew Smathers

Owner: set to Drew Smathers
Status: newassigned

comment:5 Changed 6 years ago by Jean-Paul Calderone

Author: exarkun
Branch: branches/filebodyproducer-4017

(In [32067]) Branching to 'filebodyproducer-4017'

comment:6 Changed 6 years ago by Jean-Paul Calderone

Keywords: review added
Owner: Drew Smathers deleted
Status: assignednew

I took a crack at this because I thought it would be quick and easy. Oh well, I've certainly done worse than none out of two before.

Please find FileBodyProducer, tests, docs, and an example in the linked branch. Here are the build results.

comment:7 Changed 6 years ago by Jean-Paul Calderone

Description: modified (diff)
Summary: Add an IEntityBodyProducer which produces bytes from a file-like objectAdd an IBodyProducer which produces bytes from a file-like object

comment:8 Changed 6 years ago by Jean-Paul Calderone

One possible change that should be made to this code is that the producer should take responsibility for closing the file after it finishes reading from it or if stopProducing is called.

comment:9 Changed 6 years ago by therve

Owner: set to Jean-Paul Calderone
  • Instead of keeping a reference to the the cooperator object, you may only keep one to the cooperate method. It would prevent the need of that comment about the task module.
  • I don't have a really strong feeling for closing the file or not, but whatever you choose, it should be mentioned in the documentation (both the docstring and the howto).

Please merge!

comment:10 Changed 6 years ago by therve

Keywords: review removed

comment:11 Changed 6 years ago by Jean-Paul Calderone

(In [32154]) Address review feedback, plus

  • add docstrings to some test helpers
  • use the bound cooperate method instead of a Cooperator instance/cooperator module
  • close the input file when the FileBodyProducer is done with it (and update docs)

refs #4017

comment:12 Changed 6 years ago by Jean-Paul Calderone

Branch: branches/filebodyproducer-4017branches/filebodyproducer-4017-2

(In [32155]) Branching to 'filebodyproducer-4017-2'

comment:13 Changed 6 years ago by Jean-Paul Calderone

Resolution: fixed
Status: newclosed

(In [32157]) Merge filebodyproducer-4017-2

Author: exarkun Reviewer: therve Fixes: #4017

Add twisted.web.client.FileBodyProducer for use with Agent to specify request bodies based on file-like objects.

Note: See TracTickets for help on using tickets.