【发布时间】:2015-11-08 20:38:07
【问题描述】:
请原谅我的英语不好(我会说西班牙语)。
我正在尝试提交一个上传图片的表单。
当我想发送普通数据(文本字符串)时,我会这样做:
第一个导入模块:
import urllib.request
import urllib.parse
import http.cookiejar
准备 cookie 和标头:
cj = http.cookiejar.CookieJar()
open = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
abriendo.addheaders = [("User-agent","Mozilla/5.0")]
urllib.request.install_opener(open)
编码 url 数据,我使用 id (它是在 web 表单 html 中)
valor1 = {"username":"test1","password","hello"}
valor2 = urllib.parse.urlencode(valor1)
finalvalor = valor2.encode("UTF-8")
现在可以发送帖子数据(记住,这是一个字符串数据的例子)
nav = urllib.request.urlopen(url,finalvalor)
navread = str(nav.read())
Url 变量有帖子地址
这项工作很好,但我无法发送图像。
网络表单以以下代码开头:
<form enctype="multipart/form-data" onsubmit="return ver('espera');"
action="example.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="5300000">
<br><br>
<div align="center" id="uploadFileContainer">
<input size="25" id="uploadFile1" name="uploadFile1" type="file">
我愿意
dicc = {"uploadFile1":open("1.jpg","rb")}
nav = urllib.request.urlopen(url,dicc)
navread = str(nav.read())
有这个错误:
ValueError: Content-Length should be specified for iterable data of type <class 'dict'> {'uploadFile1': <_io.BufferedReader name='1.jpg'>
我尝试对数据进行编码( enconde dicc 如何正常 url )但不起作用(不发送任何数据)。我在谷歌上冲浪,我认为二进制数据需要编码为 base64 ??
最后的问题是,如何上传带有 urllib.request 模块的图片?
感谢阅读。
【问题讨论】:
标签: python image python-3.x post