Streaming data of unknown size from client to server over HTTP in Python

后端 未结 1 1989
情书的邮戳
情书的邮戳 2020-12-29 16:58

As unfortunately my previous question got closed for being an \"exact copy\" of a question while it definitely IS NOT, hereby again.

It is NOT a duplicate of Python:

相关标签:
1条回答
  • 2020-12-29 17:00

    From the client’s perspective, it’s easy. You can use httplib’s low-level interface—putrequest, putheader, endheaders, and send—to send whatever you want to the server in chunks of any size.

    But you also need to indicate where your file ends.

    If you know the total size of the file in advance, you can simply include the Content-Length header, and the server will stop reading your request body after that many bytes. The code may then look like this.

    import httplib
    import os.path
    
    total_size = os.path.getsize('/path/to/file')
    infile = open('/path/to/file')
    conn = httplib.HTTPConnection('example.org')
    conn.connect()
    conn.putrequest('POST', '/upload/')
    conn.putheader('Content-Type', 'application/octet-stream')
    conn.putheader('Content-Length', str(total_size))
    conn.endheaders()
    while True:
        chunk = infile.read(1024)
        if not chunk:
            break
        conn.send(chunk)
    resp = conn.getresponse()
    

    If you don’t know the total size in advance, the theoretical answer is the chunked transfer encoding. Problem is, while it is widely used for responses, it seems less popular (although just as well defined) for requests. Stock HTTP servers may not be able to handle it out of the box. But if the server is under your control too, you could try manually parsing the chunks from the request body and reassembling them into the original file.

    Another option is to send each chunk as a separate request (with Content-Length) over the same connection. But you still need to implement custom logic on the server. Moreover, you need to persist state between requests.

    Added 2012-12-27. There’s an nginx module that converts chunked requests into regular ones. May be helpful so long as you don’t need true streaming (start handling the request before the client is done sending it).

    0 讨论(0)
提交回复
热议问题