如何在Python中向上传输http2 POST请求



我想将数据从python客户端流式传输到HTTP2 POST请求。也就是说,从客户端流式传输到服务器。

我在httpx文档中找到了一个示例,它显示了如何从响应流式传输。我想做相反的事情,在POST请求中将向上流式传输到服务器。

我来自javascript背景,其中请求对象是一个可写流,所以我可以做这样的事情:

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

如何在Python中实现类似的功能?

我认为我可以根据github部分中的httpx问题管理它向上流到某个主机,并进行一点修改以上传txt文件

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

最新更新