使用urllib2上传图像文件时的无效URL消息

0 投票
2 回答
1162 浏览
提问于 2025-04-16 02:53

我正在尝试从Python上传一个图片文件到piczasso.com。我的代码如下:

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代码有什么问题呢?

请帮帮我,谢谢!

2 个回答

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

在这个PHP代码中,表单里有选择图片大小的选项。这些选项的值是数字,而显示出来的选项是文字。比如,'无'这个选项的值是1。

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

撰写回答