HTTP POST binary files using Python: concise non-pycurl examples?

前端 未结 5 1218
-上瘾入骨i
-上瘾入骨i 2021-02-06 13:29

I\'m interested in writing a short python script which uploads a short binary file (.wav/.raw audio) via a POST request to a remote server.

I\'ve done this with pycurl,

5条回答
  •  暖寄归人
    2021-02-06 14:00

    POSTing a file requires multipart/form-data encoding and, as far as I know, there's no easy way (i.e. one-liner or something) to do this with the stdlib. But as you mentioned, there are plenty of recipes out there.

    Although they seem verbose, your use case suggests that you can probably just encapsulate it once into a function or class and not worry too much, right? Take a look at the recipe on ActiveState and read the comments for suggestions:

    • Recipe 146306: Http client to POST using multipart/form-data

    or see the MultiPartForm class in this PyMOTW, which seems pretty reusable:

    • PyMOTW: urllib2 - Library for opening URLs.

    I believe both handle binary files.

提交回复
热议问题