使用POST从Python发送文件 [英] Send file using POST from a Python

查看:648
本文介绍了使用POST从Python发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址,我需要在其中发送视频文件. 因此,我编写了以下代码:

I have the url, where I need to send a video file. For this reason i wrote this code:

import requests

upload_url = 'https://cs506200.vk.me/upload_video_new.php?act=add_video&mid=21844505&oid=21844505&vid=171170813&fid=0&tag=93bb46ee&hash=e238f469a32fe7eee85a&swfupload=1&api=1'
file_ = {'file': ('video.mp4', open('video.mp4', 'rb'))}
r = requests.post(upload_url, files=file_)

print (r.text)

我得到一个错误: {错误":无效的文件"}

I get an error: {"error":"invalid file"}

但是在这种情况下:

<!DOCTYPE HTML>
<html>
 <head>
  <meta charset="utf-8">
 </head>
 <body>  
<form enctype="multipart/form-data" action="https://cs506200.vk.me/upload_video_new.php?act=add_video&mid=21844505&oid=21844505&vid=171170813&fid=0&tag=93bb46ee&hash=e238f469a32fe7eee85a&swfupload=1&api=1" method="POST" target="_blank">

<input type="file" name="video_file" />

<input type="submit" value="submit" name="submit" />
</form>
 </body>
</html>

一切正常. 我在做什么错了?

All working fine. What am I doing wrong?

推荐答案

您使用了错误的字段名称:

You are using the wrong field name:

file_ = {'file': ('video.mp4', open('video.mp4', 'rb'))}

在表单使用video_file时将其命名为file字段:

That names the field file while your form uses video_file:

<input type="file" name="video_file" />

使用正确的字段名称很重要,请更正您的参数:

Using the right field name is important, correct your parameters:

file_ = {'video_file': ('video.mp4', open('video.mp4', 'rb'))}

这篇关于使用POST从Python发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆