Skip to content

Instantly share code, notes, and snippets.

@alswl
Created September 29, 2017 06:06
Show Gist options
  • Save alswl/c6728a6532b147fcbc3cb4d37bc10428 to your computer and use it in GitHub Desktop.
Save alswl/c6728a6532b147fcbc3cb4d37bc10428 to your computer and use it in GitHub Desktop.
# 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', '')
# 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)
@alswl
Copy link
Author

alswl commented Sep 29, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment