【问题标题】:Upload an image file using python使用python上传图像文件
【发布时间】:2019-09-23 22:20:54
【问题描述】:

有没有办法使用 python 上传图像文件?我能够从包含以下代码的简单 html 页面上传文件。但我希望能够从 python 程序中做到这一点。可以使用 urllib2 模块完成吗?有什么可以参考的例子吗?

请帮忙。 谢谢。

<form action="http://somesite.com/handler.php" method="post" enctype="multipart/form-data">

<table>

        <tr><td>File:</td><td><input type="file" name="file" /></td></tr>

        <tr><td><input type="submit" value="Upload" /></td></tr>

</table>

</form>

【问题讨论】:

  • 什么?您想要handler.php 的版本吗?您是否搜索过类似的问题?很多问题都和你一样。
  • upload file via python script 的可能重复项

标签: python


【解决方案1】:

您可以为此使用 pycurl 包。

from StringIO import StringIO
from pycurl import *
c = Curl()
d = StringIO()
h = StringIO()
c.setopt(URL, "http://somesite.com/handler.php")
c.setopt(POST, 1)
c.setopt(HTTPPOST, [('file', (FORM_FILE, '/path/to/file')), ('submit', 'Upload')])
c.setopt(WRITEFUNCTION, d.write)
c.setopt(HEADERFUNCTION, h.write)
c.perform()
c.close()

【讨论】:

  • 你可以使用 pycurl 从内存而不是文件中发布二进制数据吗?
【解决方案2】:
猜你喜欢
  • 2016-10-19
  • 2014-08-17
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
  • 2014-07-14
  • 2016-02-03
相关资源
最近更新 更多