如何使用python上传包含图像的表单?

时间:2012-06-20 03:41:31

标签: python http post

我无法做到这一点......

您可能希望获得与http://i.joyton.com:2010中相同的结果 使用此图像。enter image description here和其他参数保持默认值。

def search_img(item, image_name):
try:
    f = open(image_name, 'rb')
    img = f.read()
    print type(img)
except IOError, e:
    print 'fail to open %s' % image_name
    print e
    return None

ts = str(time.time())

m = md5.new('testsearch_by_image' + item)
m.update(ts)
m.update('0123456789')
sign = m.hexdigest()

params = urllib.urlencode( {
    'item': item,
    'app_key': 'test',
    'cmd':'search_by_image',
    'sign':sign,
    'img_file':img,
    'extra':'',
    'time_stamp':ts,
    })

headers = {'Content-type': 'application/x-www-form-urlencode',
           'Accept': 'text/plain'}

conn = httplib.HTTPConnection('i.joyton.com', 2010)
conn.request('POST', '', params, headers)
response = conn.getresponse()

print response.status, response.reason
print response.read()
conn.close()
return response.read()

if __name__ == '__main__':
    search_img('book', 'f:\\book_001.jpg')

在浏览器中,一切都很完美,但我的脚本没有。有时脚本会返回正确的结果;有时它会得到其他书籍,有时它什么都没有。当它获得其他书籍时,这些书籍最近经常被其他人搜索。

1 个答案:

答案 0 :(得分:2)

以下是您修改后的代码multipart/form-data。虽然这不起作用,但当我在我的电脑上测试你的网址时,它可能需要一些黑客攻击(可能是标志不正确或某些东西),然后才能让它工作。

import mimetypes
import string
import random
import time
import md5
import httplib

def upload(fields,files):
    boundaryChars = list(string.lowercase) + list(string.uppercase) + \
                    [str(x) for x in range(10)] + ['_'*10]
    random.shuffle(boundaryChars)    

    boundary = '----------RaNdOm_crAPP'+''.join(boundaryChars[:20])
    CRLF = '\r\n'
    elem = []
    for key in fields:
        elem.append('--' + boundary)
        elem.append('Content-Disposition: form-data; name="%s"' % key)
        elem.append('')
        elem.append(fields[key])
    for (key, filename,value) in files:
        elem.append('--' + boundary)
        elem.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        elem.append('Content-Type: %s' % mimetypes.guess_type(filename)[0] or \
                        'application/octet-stream')
        elem.append('')
        elem.append(value)
    elem.append('--' + boundary + '--')
    elem.append('')
    body = CRLF.join(elem)
    content_type = 'multipart/form-data; boundary=%s' % boundary
    return content_type, body

def search_img(item, image_name):
    try:
        f = open(image_name, 'rb')
        img = f.read()
    except IOError, e:
        print 'fail to open %s' % image_name
        print e
        return None

    ts = str(time.time())

    m = md5.new('testsearch_by_image' + item)
    m.update(ts)
    m.update('0123456789')
    sign = m.hexdigest()

    #params = urllib.urlencode( )

    contentType,body = upload({
        'item': item,
        'app_key': 'test',
        'cmd':'search_by_image',
        'sign':sign,
        #'img_file':img,
        'extra':'',
        'time_stamp':ts,
        },
        [('img_file', image_name, img)]
    )
    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Content-type': contentType,
        'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5',
        'Host':'i.joyton.com:2010',
        'Origin':'http://i.joyton.com:2010',
        'Referer':'http://i.joyton.com:2010/'
    }
    #print c
    #print body

    conn = httplib.HTTPConnection('i.joyton.com', 2010)
    conn.request('POST', '/', body, headers)
    response = conn.getresponse()

    print response.status, response.reason
    print response.read()
    conn.close()
    return response.read()

if __name__ == '__main__':
    search_img('book', 'iMgXS.jpg') #the same image.