2010-08-16 67 views
2

有没有使用python上传图片文件的方法?我能够从下面的代码组成的简单的HTML页面上传文件。但我希望能够从一个Python程序做到这一点。可以使用urllib2模块来完成吗?我可以参考的任何例子?使用python上传图片文件

请帮忙。 谢谢。

<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> 
+1

什么?你想要一个'handler.php'版本吗?你有没有搜索类似的问题?许多问题与您的问题相同。 – 2010-08-16 22:40:33

+0

可能重复[上传文件通过python脚本](http://stackoverflow.com/questions/1993060/upload-file-via-python-script) – 2010-08-16 22:40:56

回答

5

可以使用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() 
+0

你可以从内存,而不是使用pycurl文件发布二进制数据? – Naveen 2011-01-28 20:11:01