2010-08-16 68 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