Skip to content
This repository was archived by the owner on Jun 29, 2026. It is now read-only.

Commit f481d1c

Browse files
committed
modify doc
1 parent c22812b commit f481d1c

13 files changed

Lines changed: 333 additions & 228 deletions

File tree

README.rst

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,125 @@ Gt Python SDK
22
===============
33

44
极验验证的Python SDK目前提供基于django, flask, tornado框架的DEMO
5-
本项目提供的Demo的前端实现方法均是面向PC端的。 如果需要移动端的canvas功能,请参考canvas的 `前端文档 <http://www.geetest.com/install/>`_.
5+
本项目是面向服务器端的,客户端相关开发请参考我们的 `前端文档 <http://www.geetest.com/install/>`_.
66

77
开发环境
8-
_______________
8+
----------------
99

1010
- Python (推荐2.7.0以上版本)
1111
- django, flask, tornado框架
1212

1313
快速开始
14-
_______________
14+
---------------
1515

16-
1. 从 `Github <https://github.com/GeeTeam/gt-python-sdk/>`__ 上Clone代码:
16+
下面使用示例代码的均以flask框架为例.
17+
18+
1. 获取代码
19+
20+
从 `Github <https://github.com/GeeTeam/gt-python-sdk/>`__ 上Clone代码:
1721

1822
.. code-block:: bash
1923
2024
$ git clone https://github.com/GeeTeam/gt-python-sdk.git
2125
22-
2. django demo运行:进入django_demo文件夹,运行:
26+
2. 安装GeetestSDK
2327

2428
.. code-block:: bash
2529
26-
$ python start.py runserver 0.0.0.0:8000
30+
$ sudo python setup.py install
2731
28-
在浏览器中访问http://localhost:8000即可看到Demo界面
32+
3. 初始化验证
2933

30-
3. flask demo运行:进入flask_demo文件夹,运行:
3134

32-
.. code-block:: bash
35+
在调用GeetestLib前请自行设定公钥和私钥:
3336

34-
$ python start.py
37+
.. code-block :: python
3538
36-
在浏览器中访问http://localhost:5000即可看到Demo界面
37-
38-
4. tornado demo运行:进入tornado_demo文件夹,运行:
39+
captach_id = "你的公钥"
40+
private_key = "你的私钥"
3941
40-
.. code-block:: bash
42+
根据自己的私钥出初始化验证
4143

42-
$ python start.py
44+
.. code-block :: python
4345
44-
在浏览器中访问http://localhost:8088即可看到Demo界面
46+
@app.route('/getcaptcha', methods=["GET"])
47+
def get_captcha():
48+
gt = GeetestLib(captach_id, private_key)
49+
status, response_str = gt.pre_process()
50+
session[gt.GT_STATUS_SESSION_KEY] = gt
51+
return response_str
4552
53+
上述代码是一般验证初始化的代码,因为现在我们服务提供完备的服务宕机方案,所以推荐直接使用我们的宕机方案,也可以换成你们自己的方案,根据返回的`status` 自行处理.
4654

47-
SDK 使用说明
48-
_________________
55+
4. 二次验证
4956

50-
以django为例
57+
.. code-block :: python
5158
52-
1. 核心SDK库: ../python_sdk/geetest.py
59+
@app.route('/validate', methods=["POST"])
60+
def validate_capthca():
61+
gt = GeetestLib(captcha_id, private_key)
62+
status = session[gt.GT_STATUS_SESSION_KEY]
63+
challenge = request.form[gt.FN_CHALLENGE]
64+
validate = request.form[gt.FN_VALIDATE]
65+
seccode = request.form[gt.FN_SECCODE]
66+
gt = GeetestLib(captcha_id, private_key)
67+
result = gt.validate(status, challenge, validate, seccode)
68+
return result
5369
54-
2. api文档: ../doc/api.rst
70+
如果不想采用极验提供的failback方案,你可以自己处理,代码如下
5571

56-
3. 公钥和私钥初始化:查看../demo/django_demo/app/views.py
72+
.. code-block :: python
5773
58-
.. code-block:: python
74+
@app.route('/validate', methods=["POST"])
75+
def validate_capthca():
76+
status = session[GeetestLib.GT_STATUS_SESSION_KEY]
77+
if status:
78+
gt = GeetestLib(captcha_id, private_key)
79+
challenge = request.form[gt.FN_CHALLENGE]
80+
validate = request.form[gt.FN_VALIDATE]
81+
seccode = request.form[gt.FN_SECCODE]
82+
result = gt.success_validat(challenge, validate, seccode)
83+
else:
84+
#你们自己的验证方法
85+
return result
5986
60-
captcha_id ="你的公钥"
61-
private_key = "你的私钥"
87+
运行demo
88+
---------------------
6289

63-
4. 请求验证码时使用register_challenge()获取challenge
90+
1. django demo运行:进入django_demo文件夹,运行:
6491

65-
.. code-block:: python
92+
.. code-block:: bash
6693
67-
gt = geetest.GeetestLib(captcha_id, private_key)
68-
challenge = gt.register_challenge()
94+
$ python manage.py runserver 0.0.0.0:8000
6995
70-
5. 预处理和session控制
96+
在浏览器中访问http://localhost:8000即可看到Demo界面
7197

72-
.. code-block:: python
98+
2. flask demo运行:进入flask_demo文件夹,运行:
7399

74-
gt = GeetestLib(captcha_id, private_key)
75-
if gt.pre_process():
76-
res_str = gt.success_pre_process()
77-
gt.set_gtserver_session(session.__setitem__, 1)
78-
else: #宕机情况下提供failback方案,可自行更换
79-
res_str = gt.fail_pre_process()
80-
gt.set_gtserver_session(session.__setitem__, 0)
81-
return res_str
100+
.. code-block:: bash
82101
83-
6. validate验证:
102+
$ python start.py
103+
104+
在浏览器中访问http://localhost:5000即可看到Demo界面
84105

85-
.. code-block:: python
106+
3. tornado demo运行:进入tornado_demo文件夹,运行:
107+
108+
.. code-block:: bash
109+
110+
$ python start.py
111+
112+
在浏览器中访问http://localhost:8088即可看到Demo界面
86113

87-
if request.method == "POST":
88-
challenge = request.POST.get('geetest_challenge', '')
89-
validate = request.POST.get('geetest_validate', '')
90-
seccode = request.POST.get('geetest_seccode', '')
91-
gt = geetest.GeetestLib(captcha_id, private_key)
92-
gt_challenge = gt.get_gtserver_challenge(request.session.__getitem__)
93-
gt_server_status = gt.get_gtserver_session(request.session.__getitem__)
94-
if gt_server_status == 1:
95-
result = gt.post_validate(challenge, validate, seccode)
96-
else:
97-
result = gt.failback_validate(challenge, validate, seccode)
98-
return HttpResponse(result)
99-
return HttpResponse("error")
100114

101115
发布日志
102-
_______________
116+
-----------------
117+
+[3.0.0]
118+
- 去除SDK对Session操作, 现在Session部分由开发者自己处理
119+
- 简易化初始化过程.
120+
- 修复failback模式BUG
121+
103122
+[2.0.2]
104123
- 添加通过session控制的challenge检查
105-
- Date : 2015.12.30
124+
106125
+[2.0.1]
107126
- SDK库和django和flask demo重制
108-
- Date : 2015.12.24

demo/django_demo/app/views.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding:utf-8
22
from django.shortcuts import render_to_response, RequestContext
33
from django.http import HttpResponse
4-
import sdk.geetest as geetest
4+
from geetest import GeetestLib
55

66
BASE_URL = "api.geetest.com/get.php?gt="
77
captcha_id = "a40fd3b0d712165c5d13e6f747e948d4"
@@ -12,38 +12,21 @@
1212
# product = "popup&popupbtnid=submit-button"
1313

1414
def home(request):
15-
gt = geetest.GeetestLib(captcha_id, private_key)
16-
url = ""
17-
httpsurl = ""
18-
try:
19-
challenge = gt.register_challenge()
20-
except:
21-
challenge = ""
22-
if len(challenge) == 32:
23-
url = "http://%s%s&challenge=%s&product=%s" % (BASE_URL, captcha_id, challenge, product)
24-
return render_to_response("index.html", {"url": url}, context_instance=RequestContext(request))
15+
return render_to_response("index.html", context_instance=RequestContext(request))
2516

26-
def login(request):
27-
gt = geetest.GeetestLib(captcha_id, private_key)
28-
if gt.pre_process():
29-
res_str = gt.success_pre_process()
30-
gt.set_gtserver_session(request.session.__setitem__, 1, gt.challenge) #request.session['status'] = 1
31-
else:
32-
res_str = gt.fail_pre_process()
33-
gt.set_gtserver_session(request.session.__setitem__, 0, gt.challenge) #request.session['status'] = 0
34-
return HttpResponse(res_str)
17+
def getcaptcha(request):
18+
gt = GeetestLib(captcha_id, private_key)
19+
status, response_str = gt.pre_process()
20+
request.session[gt.GT_STATUS_SESSION_KEY] = status
21+
return HttpResponse(response_str)
3522

3623
def validate(request):
3724
if request.method == "POST":
38-
challenge = request.POST.get('geetest_challenge', '')
39-
validate = request.POST.get('geetest_validate', '')
40-
seccode = request.POST.get('geetest_seccode', '')
41-
gt = geetest.GeetestLib(captcha_id, private_key)
42-
gt_challenge = gt.get_gtserver_challenge(request.session.__getitem__)
43-
gt_server_status = gt.get_gtserver_session(request.session.__getitem__)
44-
if gt_server_status == 1:
45-
result = gt.post_validate(challenge, validate, seccode)
46-
else:
47-
result = gt.failback_validate(challenge, validate, seccode)
25+
gt = GeetestLib(captcha_id, private_key)
26+
challenge = request.POST.get(gt.FN_CHALLENGE, '')
27+
validate = request.POST.get(gt.FN_VALIDATE, '')
28+
seccode = request.POST.get(gt.FN_SECCODE, '')
29+
status = request.session[gt.GT_STATUS_SESSION_KEY]
30+
result = gt.validate(status, challenge, validate, seccode)
4831
return HttpResponse(result)
4932
return HttpResponse("error")

demo/django_demo/db.sqlite3

0 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.conf.urls import patterns, url
22

33
urlpatterns = patterns('',
4-
url(r'^login$', 'app.views.login', name='login'),
4+
url(r'^getcaptcha$', 'app.views.getcaptcha', name='getcaptcha'),
55
url(r'^validate$', 'app.views.validate', name='validate'),
66
url(r'/*', 'app.views.home', name='home'),
77
)

demo/django_demo/static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
})()
145145

146146
$.ajax({
147-
url : "/login",
147+
url : "/getcaptcha",
148148
type : "get",
149149
dataType : 'JSON',
150150
success : function(result) {

demo/flask_demo/templates/login.html

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<head>
33
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
4-
<title>极验行为式验证 flask 类网站安装测试页面</title>
4+
<title>极验行为式验证 Flask 类网站安装测试页面</title>
55
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
66
</head>
77
<body>
@@ -17,7 +17,7 @@
1717
}
1818
.box{
1919
width:300px;
20-
margin: 30px auto;
20+
margin: 30px auto;
2121
}
2222
.header{
2323
margin: 80px auto 30px auto;
@@ -36,8 +36,8 @@
3636
border: 1px solid #FFFFF0;
3737
background-color: #31C552;
3838
border-radius: 4px;
39-
font-size: 14px;
40-
color: #FFFFF0;
39+
font-size: 14px;
40+
color: #FFFFF0;
4141
}
4242
</style>
4343

@@ -63,29 +63,29 @@
6363
<button id="submit-button">提交</button>
6464
</div>
6565
<script type="text/javascript">
66-
66+
6767

6868
function geetest_ajax_results() {
6969
//TODO, not necessory a geetest ajax demo,
7070
$.ajax({
71-
url : "todo yourself",//todo:set the servelet of your own
71+
url : "",//todo:set the servelet of your own
7272
type : "post",
7373
data : gt_captcha_obj.getValidate(),
7474
success : function(sdk_result) {
7575
console.log(sdk_result)
7676
}
7777
});
7878
}
79-
80-
79+
80+
8181
var gtFailbackFrontInitial = function(result) {
8282
var s = document.createElement('script');
8383
s.id = 'gt_lib';
8484
s.src = 'http://static.geetest.com/static/js/geetest.0.0.0.js';
8585
s.charset = 'UTF-8';
8686
s.type = 'text/javascript';
8787
document.getElementsByTagName('head')[0].appendChild(s);
88-
var
88+
var
8989
loaded = false;
9090
s.onload = s.onreadystatechange = function() {
9191
if (!loaded
@@ -111,16 +111,16 @@
111111

112112
gt_captcha_obj.appendTo("#div_id_embed");
113113

114-
//Ajax request demo,if you use submit form ,then ignore it
114+
//Ajax request demo,if you use submit form ,then ignore it
115115
gt_captcha_obj.onSuccess(function() {
116116
geetest_ajax_results()
117117
});
118118
}
119119

120-
//s = document.createElement('script');
121-
//s.src = 'http://api.geetest.com/get.php?callback=gtcallback';
122-
//$("#div_geetest_lib").append(s);
123-
120+
s = document.createElement('script');
121+
s.src = 'http://api.geetest.com/get.php?callback=gtcallback';
122+
$("#div_geetest_lib").append(s);
123+
124124
var gtcallback =( function() {
125125
var status = 0, result, apiFail;
126126
return function(r) {
@@ -142,7 +142,7 @@
142142
}
143143
}
144144
})()
145-
145+
146146
$.ajax({
147147
url : "/getcaptcha",
148148
type : "get",
@@ -152,7 +152,7 @@
152152
}
153153
})
154154
</script>
155-
</div>
155+
</div>
156156
</form>
157157
</div>
158158
</div>

0 commit comments

Comments
 (0)