Python海报内容长度

2024-10-03 06:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用poster模块发送图像。我学了这个例子,但对我不管用

我的代码:

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib, urllib2

def decaptcha(hash):
register_openers()

    params = {
        "file": open("captcha.jpg", "rb"),
        "function" : "picture2",
        "username" : "uname",
        "password" : "pwd",
        "pict_to" : 0,
        "pict_type" : 0
        }


    datagen, headers = multipart_encode(params)

    req = urllib2.Request("http://poster.decaptcher.com/")

    solve = urllib2.urlopen(req, datagen, headers)
    print solve.read()

decaptcha(None)

以及回溯:

^{pr2}$

Tags: fromimportregisterparamsurllib2reqencodeheaders
2条回答

您应该将datagen和headers传递给请求,而不是urlopen:

req = urllib2.Request("http://poster.decaptcher.com/", datagen, headers)
solve = urllib2.urlopen(req)

(免责声明:我没有使用海报库。建议的解决方案是我的最佳猜测。)

从海报上看,这似乎是可行的。在

我将尝试以下方法(传递文件的内容而不是打开文件迭代器,应该可以解决iterable body问题):

params = {
    "file": open("captcha.jpg", "rb").read(),
    "function" : "picture2",
    "username" : "uname",
    "password" : "pwd",
    "pict_to" : 0,
    "pict_type" : 0
    }

建议2:

或者试试: 从多部分编码导入MultiPartParam

^{pr2}$

如果由于相同的错误而失败,请尝试将filesize参数指定给MultiPartParam。在

相关问题 更多 >