Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalid URL message when uploading an image file using urllib2
    primarykey
    data
    text
    <p>I am trying to upload an image file to piczasso.com from python. My code is as follows:</p> <pre><code>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" </code></pre> <p>However,im getting "Invalid URL!" as the output. I am however able to upload the image using a php page as follows. </p> <pre><code>&lt;?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("+", "&amp;nbsp;", htmlentities($image["html"])); echo " &lt;img src=\"{$image["thumb"]}\" alt=\"\" /&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt;HTML for Websites:&lt;/td&gt;&lt;td&gt;&lt;input type=\"text\" name=\"codes1\" value=\"{$image["html"]}\" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;IMG Code for Forums:&lt;/td&gt;&lt;td&gt;&lt;input type=\"text\" name=\"codes2\" value=\"{$image["bbcode"]}\" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;URL for E-Mail:&lt;/td&gt;&lt;td&gt;&lt;input type=\"text\" name=\"codes3\" value=\"{$image["image_page"]}\" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Direct Link for Layouts:&lt;/td&gt;&lt;td&gt;&lt;input type=\"text\" name=\"codes4\" value=\"{$image["direct"]}\" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; "; } else { if(isset($_GET["error"])){ /* Possible errors. Complete error list: http://www.piczasso.com/api_errorcodes.txt */ $errorcodes = array( 1000 =&gt; "No image selected", 1001 =&gt; "Image failed to upload", 1002 =&gt; "Invalid image type", 1003 =&gt; "Image is larger than 16MB", 1004 =&gt; "Cannot read image info", 1005 =&gt; "Upload failed during process", 1006 =&gt; "Database error", 1007 =&gt; "Banned IP" ); echo $errorcodes[$_GET["error"]]; } /* Sample upload form below. Image can be resized also by sending for example this kind of fields: &lt;tr&gt;&lt;td&gt;Resize x:&lt;/td&gt;&lt;td&gt;&lt;input type=\"text\" name=\"size_x\"/&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Resize y:&lt;/td&gt;&lt;td&gt;&lt;input type=\"text\" name=\"size_y\"/&gt;&lt;/td&gt;&lt;/tr&gt; */ echo " &lt;form action=\"http://piczasso.com/api_handler.php\" method=\"post\" enctype=\"multipart/form-data\"&gt; &lt;input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"99999999\" /&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt;File:&lt;/td&gt;&lt;td&gt;&lt;input type=\"file\" name=\"file\" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Tags:&lt;/td&gt;&lt;td&gt;&lt;input type=\"text\" name=\"tags\" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Resize:&lt;/td&gt;&lt;td&gt; &lt;select name=\"size\"&gt; &lt;option value=\"1\" selected=\"selected\" &gt;None&lt;/option&gt; &lt;option value=\"2\" &gt;100x75 Avatar&lt;/option&gt; &lt;option value=\"3\" &gt;150x112 Thumbnail&lt;/option&gt; &lt;option value=\"4\" &gt;320x240 Websites&lt;/option&gt; &lt;option value=\"5\" &gt;640x480 For forums&lt;/option&gt; &lt;option value=\"6\"&gt;800x600 15-inch monitor&lt;/option&gt; &lt;option value=\"7\" &gt;1024x768 17-inch monitor&lt;/option&gt; &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;input type=\"submit\" value=\"send\" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; "; } ?&gt; </code></pre> <p>What is the problem with my python code ?</p> <p>Please help Thank You</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload