2010-08-17 57 views
0

我想從python上傳圖像文件到piczasso.com。我的代碼如下:使用urllib2上傳圖像文件時出現無效的URL消息

import urllib,urllib2 

url='http://piczasso.com/api_handler.php' 
values = { 'data':open('./imagefile.jpg'), 
      'tags':'', 
      'size':'None' 
} 

data = urllib.urlencode(values) 
req = urllib2.Request(url,data) 
try: 
     response = urllib2.urlopen(req).read() 
     print response 
except urllib2.URLError, e: 
     print e.code 
     print e.read() 

print "done" 

但是,我得到「無效的URL!」作爲輸出。然而,我能夠使用php頁面上傳圖像,如下所示。

<?php 

/* 

Simple sample script which uses Piczasso API 

Documentation about PicZasso API can be found here: http://www.piczasso.com/api.php 

*/ 



if(isset($_GET["image"])){ 

    /* API sends output in JSON format */ 

    $image = json_decode(rawurldecode($_GET["image"]), true); 

    /* 

    $image array includes information about the image URL and sample links to it: 

     $image["direct"] = Image URL 

     $image["thumb"] = Thumbnail URL 

     $image["image_page"] = Link to image 

     $image["bbcode"] = bbcode for forums 

     $image["html"] = html code 

    Sample table below with image links. 

    */ 

    $image["html"] = str_replace("+", "&nbsp;", htmlentities($image["html"])); 

    echo " 

    <img src=\"{$image["thumb"]}\" alt=\"\" /> 

    <table> 

     <tr><td>HTML for Websites:</td><td><input type=\"text\" name=\"codes1\" value=\"{$image["html"]}\" /></td></tr> 

     <tr><td>IMG Code for Forums:</td><td><input type=\"text\" name=\"codes2\" value=\"{$image["bbcode"]}\" /></td></tr> 

     <tr><td>URL for E-Mail:</td><td><input type=\"text\" name=\"codes3\" value=\"{$image["image_page"]}\" /></td></tr> 

     <tr><td>Direct Link for Layouts:</td><td><input type=\"text\" name=\"codes4\" value=\"{$image["direct"]}\" /></td></tr> 

    </table> 

    "; 

} else { 

    if(isset($_GET["error"])){ 

    /* Possible errors. Complete error list: http://www.piczasso.com/api_errorcodes.txt */ 

     $errorcodes = array(

      1000 => "No image selected", 

      1001 => "Image failed to upload", 

      1002 => "Invalid image type", 

      1003 => "Image is larger than 16MB", 

      1004 => "Cannot read image info", 

      1005 => "Upload failed during process", 

      1006 => "Database error", 

      1007 => "Banned IP" 

     ); 

     echo $errorcodes[$_GET["error"]]; 

    } 

    /* Sample upload form below. Image can be resized also by sending for example this kind of fields: 

     <tr><td>Resize x:</td><td><input type=\"text\" name=\"size_x\"/></td></tr> 

     <tr><td>Resize y:</td><td><input type=\"text\" name=\"size_y\"/></td></tr> 

    */ 

    echo " 

    <form action=\"http://piczasso.com/api_handler.php\" method=\"post\" enctype=\"multipart/form-data\"> 

    <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"99999999\" /> 

    <table> 

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

     <tr><td>Tags:</td><td><input type=\"text\" name=\"tags\" /></td></tr> 

     <tr><td>Resize:</td><td> 

     <select name=\"size\"> 

      <option value=\"1\" selected=\"selected\" >None</option> 

      <option value=\"2\" >100x75 Avatar</option> 

      <option value=\"3\" >150x112 Thumbnail</option> 

      <option value=\"4\" >320x240 Websites</option> 

      <option value=\"5\" >640x480 For forums</option> 

      <option value=\"6\">800x600 15-inch monitor</option> 

      <option value=\"7\" >1024x768 17-inch monitor</option> 

     </select></td></tr> 

     <tr><td><input type=\"submit\" value=\"send\" /></td></tr> 

    </table> 

    </form> 

    "; 

} 

?> 

我的python代碼有什麼問題?

請幫助 謝謝

+0

是從Python代碼和PHP頁面相同的HTTP請求?可能會有一些標題丟失。你可能想檢查一下。 – Gangadhar 2010-08-17 00:18:30

+0

即時消息傳遞的HTTP請求中的文件名,標籤和大小值..有什麼我需要通過? – James 2010-08-17 00:35:21

回答

1

在PHP代碼,形式有圖片大小選項。值是數字,而顯示的選項是文本。爲「無」的值是1

values = { 'data':open('./imagefile.jpg'), 
      'tags':'', 
      'size':'1' 
} 
+0

謝謝傑夫的回覆。我做了更改,但仍然收到'無效的URL!' – James 2010-08-17 00:53:37

+0

這沒有什麼幫助,那麼,是嗎? :)我注意到別的東西。看看我的新答案。 – 2010-08-17 01:09:49

-1
import urllib2_file 
import urllib2 

data = { 
     'apikey': '**********************************—0',#your APIKey 
     'imgurl' : "www.example.net/img/dog1.jpg", 
     'catid','0', 
     'subject':'', 
     'labeland':'', 
     'labelor':'', 
     'labelnot':'' 
     } 
f = urllib2.urlopen('http://api0.example.com:/api/v1.0/apisim_search', data) 
content = f.read() 
print(content) 
+0

試試上面的例子。它來自這個網站http://www.visualsearchapi.com。完整的代碼鏈接在這裏http://www.visualsearchapi.com/apiDoc/apiDoc?apiDoc=V。祝你好運。 – light45 2016-09-04 11:30:08

+0

除了缺少上下文之外,這甚至不是語法上有效的python – ppperry 2016-09-04 12:08:39

相關問題