如何将 curl -F 命令转换为带有请求的 python 代码?

How to convert curl -F command into python code with requests?

我正在使用 api 进行开发,但是我遇到了一个我从未遇到过的问题。

curl -F "media=@IMAGE_NAME" 'xxxx url'

如何将其转换为带有请求的 python 代码?

手册中有一个 great example 的 POST 请求。我认为你的具体情况是:

r = requests.post("xxx url", data={"media": "@IMAGE_NAME"})

根据您的情况:

def command_string_generator(method, token, port, account,container=None, obj=None, headers_in=None, curlhead=None):
    url = 'xxxx url'
    image = "media=@IMAGE_NAME"
    command_string = 'curl -F %s %s ' % (image ,url)
    print command_string
    return command_string

您可以根据您的需要使用这样的函数 -F 并相应地进行更改。希望这可能是 helpful.Additional 参数只是为了方便。

我解决了这个问题。

files = { 'media': open(image_name, 'rb') }

然后

requests.post(url, files=files)

http://www.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file