1. 程式人生 > >雲打碼

雲打碼

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
YDMHttp類
 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))
使用者資訊程式碼