I have trouble doing this...
You may want to get the same result as in http://i.joyton.com:2010
Using this image.
, and other parameters keep default.
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')
In the browser everything works perfectly, but my script does not. Sometimes the script returns the right result; sometimes it gets other books, sometimes it gets nothing at all. When it gets other books, these books are usually searched by others recently.
mechanizeis another way to achieve this kind of thing in python without having to hang your browser from puppetstrings.