-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha.py
64 lines (51 loc) · 1.79 KB
/
captcha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from wtforms import ValidationError
import urllib3
import json
class ImageCaptcha:
def __init__(self):
self.http = urllib3.PoolManager()
def get_json(self):
res = self.http.request('GET', '127.0.0.1:6633/generate_json')
return json.loads(res.data.decode('utf-8'), encoding='utf-8')
def check(self, puzzle_id, answer):
if answer is None or len(answer) == 0:
return False
res = self.http.request('GET',
'127.0.0.1:6633/check_json/' + puzzle_id + '/' + answer)
return json.loads(res.data.decode('utf-8'), encoding='utf-8')['pass']
@staticmethod
def get_puzzle_id(puzzle):
return puzzle['id']
@staticmethod
def get_question(puzzle):
return puzzle['question']
@staticmethod
def get_options(puzzle):
choices = []
options_arr = puzzle['options']
for option_dic in options_arr:
choices.append((option_dic['id'],
'<img src="data:image/jpeg;charset=utf-8;base64,' + option_dic['base64'] + '"/>'))
return choices
def test(self):
# call generate json
options = [{
'base64' : '...',
'id' : '1'
}, {
'base64' : '...',
'id' : '2'
}]
class VerySimpleCaptcha:
@staticmethod
def get_question():
return 'Prove you\'re a human: Who invented Bitcoin?'
@staticmethod
def check(answer):
possibilities = ['satoshi nakamoto', 'satoshi', 'nakamoto']
stopwords = ['mr', 'sir']
answer = answer.lower()
for stopword in stopwords:
answer = answer.replace(stopword, '')
if answer.strip() not in possibilities:
raise ValidationError('We don\'t know such a person.')