0

I would like to stream data from a python client to an HTTP2 POST request. Meaning, streaming from the client to the server.

I found an example on the httpx documentation that shows how to stream from a response. I would like to do the opposite, stream up to the server in a POST request.

I'm coming from a javascript background, in which the request object is a writeable stream, so I can do something like this:

process.stdin.pipe(request)
// or
pipeline(process.stdin, request)

How can I achieve something similar in Python?

Tom Klino
  • 2,358
  • 5
  • 35
  • 60
  • In that page, search for "File uploads are streaming by default". Replace the `open()` calls with `sys.stdin` – pepoluan Nov 18 '22 at 03:51

1 Answers1

0

i think i can manage it to stream upwards to a certain host based on the httpx issue in the github section and modified a little bit to upload a txt file

import httpx
import tqdm


def upload():
    with httpx.Client(http2=True) as request:
        # this will depend on what file you want to upload, 
        # for now i will just use txt files as example
        files = {'file': open('test.txt', 'rb')}
        response = request.post("https://httpbin.org/post", files=files)
        total = int(response.headers["Content-Length"])

        # im still using tqdm to display the progress bar 
        # during the upload of the file
        with tqdm.tqdm(total=total, unit_scale=True, unit_divisor=1024, unit="B") as progress:
            for chunk in response.iter_bytes():
                progress.update(len(chunk))