Created
September 29, 2017 06:06
-
-
Save alswl/c6728a6532b147fcbc3cb4d37bc10428 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from __future__ import unicode_literals, absolute_import, print_function | |
import logging | |
from io import BytesIO | |
import base64 | |
import requests | |
from PIL import Image | |
logger = logging.getLogger(__name__) | |
APP_CODE = 'PUT-YOUR-CODE-HERE' | |
def convert_checkcode_via_aliyun(image): | |
image_b64 = b64_encode_image(image) | |
response = requests.post( | |
'http://ali-checkcode.showapi.com/checkcode', | |
data={ | |
'convert_to_jpg': '1', | |
'img_base64': image_b64, | |
'typeId': '3060', | |
}, headers={ | |
'Authorization': 'APPCODE %s' % APP_CODE, | |
}) | |
assert response.status_code == 200 | |
if response.json().get('showapi_res_code') != 0: | |
logger.error(response.json()) | |
raise RuntimeError('error') | |
return response.json()['showapi_res_body']['Result'] | |
def b64_encode_image(image): | |
b = BytesIO() | |
image.save(b, 'jpeg') | |
return base64.encodestring(b.getvalue()).replace('\n', '') | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
from unittest import TestCase | |
import base64 | |
from . import checkcode_via_aliyun | |
class DevUtilTest(TestCase): | |
def test_convert_checkcode(self): | |
with open('a.png.b64', 'r') as f: | |
b64 = f.read() | |
checkcode = checkcode_via_aliyun.convert_checkcode_via_aliyun(b64) | |
self.assertEqual('832', checkcode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Buy APP Code here: https://market.aliyun.com/products/57124001/cmapi011148.html