1、云打码平台注册开发者模式用户登录
2、建立项目,下载项目案列代码接口
代码如下:
1 import http.client, mimetypes, urllib, json, time, requests 2 3 ###################################################################### 4 5 class YDMHttp: 6 7 apiurl = 'http://api.yundama.com/api.php' 8 username = '' 9 password = '' 10 appid = '' 11 appkey = '' 12 13 def __init__(self, username, password, appid, appkey): 14 self.username = username 15 self.password = password 16 self.appid = str(appid) 17 self.appkey = appkey 18 19 def request(self, fields, files=[]): 20 response = self.post_url(self.apiurl, fields, files) 21 response = json.loads(response) 22 return response 23 24 def balance(self): 25 data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey} 26 response = self.request(data) 27 if (response): 28 if (response['ret'] and response['ret'] < 0): 29 return response['ret'] 30 else: 31 return response['balance'] 32 else: 33 return -9001 34 35 def login(self): 36 data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey} 37 response = self.request(data) 38 if (response): 39 if (response['ret'] and response['ret'] < 0): 40 return response['ret'] 41 else: 42 return response['uid'] 43 else: 44 return -9001 45 46 def upload(self, filename, codetype, timeout): 47 data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)} 48 file = {'file': filename} 49 response = self.request(data, file) 50 if (response): 51 if (response['ret'] and response['ret'] < 0): 52 return response['ret'] 53 else: 54 return response['cid'] 55 else: 56 return -9001 57 58 def result(self, cid): 59 data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)} 60 response = self.request(data) 61 return response and response['text'] or '' 62 63 def decode(self, filename, codetype, timeout): 64 cid = self.upload(filename, codetype, timeout) 65 if (cid > 0): 66 for i in range(0, timeout): 67 result = self.result(cid) 68 if (result != ''): 69 return cid, result 70 else: 71 time.sleep(1) 72 return -3003, '' 73 else: 74 return cid, '' 75 76 def report(self, cid): 77 data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'} 78 response = self.request(data) 79 if (response): 80 return response['ret'] 81 else: 82 return -9001 83 84 def post_url(self, url, fields, files=[]): 85 for key in files: 86 files[key] = open(files[key], 'rb'); 87 res = requests.post(url, files=files, data=fields) 88 return res.text
1 # 用户名 2 username = 'duanhaoxin' 3 4 # 密码 5 password = 'qwer1234' 6 7 # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得! 8 appid = 6372 9 10 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得! 11 appkey = '9b672eb204d7eede7ddeda5a87d7be08' 12 13 # 图片文件 14 filename = 'getimage.jpg' 15 16 # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html 17 codetype = 1004 18 19 # 超时时间,秒 20 timeout = 10 21 22 # 检查 23 if (username == 'username'): 24 print('请设置好相关参数再测试') 25 else: 26 # 初始化 27 yundama = YDMHttp(username, password, appid, appkey) 28 29 # 登陆云打码 30 uid = yundama.login(); 31 print('uid: %s' % uid) 32 33 # 查询余额 34 balance = yundama.balance(); 35 print('balance: %s' % balance) 36 37 # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果 38 cid, result = yundama.decode(filename, codetype, timeout); 39 print('cid: %s, result: %s' % (cid, result))