Mercurial > ift6266
comparison pycaptcha/modpython_example.py @ 87:4775b4195b4b
code pour la generation de captchas
author | goldfinger |
---|---|
date | Thu, 11 Feb 2010 05:09:46 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:b3d76ebf2fac | 87:4775b4195b4b |
---|---|
1 # | |
2 # An example that presents CAPTCHA tests in a web environment | |
3 # and gives the user a chance to solve them. | |
4 # | |
5 # This example is for use with Apache using mod_python and its | |
6 # Publisher handler. For example, if your apache configuration | |
7 # included something like: | |
8 # | |
9 # AddHandler python-program .py | |
10 # PythonHandler mod_python.publisher | |
11 # | |
12 # You could place this script anywhere in your web space to see | |
13 # the demo. | |
14 # | |
15 # --Micah <micah@navi.cx> | |
16 # | |
17 | |
18 from Captcha.Visual import Tests | |
19 import Captcha | |
20 from mod_python import apache | |
21 | |
22 | |
23 def _getFactory(req): | |
24 return Captcha.PersistentFactory("/tmp/pycaptcha_%s" % req.interpreter) | |
25 | |
26 | |
27 def test(req, name=Tests.__all__[0]): | |
28 """Show a newly generated CAPTCHA of the given class. | |
29 Default is the first class name given in Tests.__all__ | |
30 """ | |
31 test = _getFactory(req).new(getattr(Tests, name)) | |
32 | |
33 # Make a list of tests other than the one we're using | |
34 others = [] | |
35 for t in Tests.__all__: | |
36 if t != name: | |
37 others.append('<li><a href="?name=%s">%s</a></li>' % (t,t)) | |
38 others = "\n".join(others) | |
39 | |
40 return """<html> | |
41 <head> | |
42 <title>PyCAPTCHA Example</title> | |
43 </head> | |
44 <body> | |
45 <h1>PyCAPTCHA Example (for mod_python)</h1> | |
46 <p> | |
47 <b>%s</b>: | |
48 %s | |
49 </p> | |
50 | |
51 <p><img src="image?id=%s"/></p> | |
52 <p> | |
53 <form action="solution" method="get"> | |
54 Enter the word shown: | |
55 <input type="text" name="word"/> | |
56 <input type="hidden" name="id" value="%s"/> | |
57 </form> | |
58 </p> | |
59 | |
60 <p> | |
61 Or try... | |
62 <ul> | |
63 %s | |
64 </ul> | |
65 </p> | |
66 | |
67 </body> | |
68 </html> | |
69 """ % (test.__class__.__name__, test.__doc__, test.id, test.id, others) | |
70 | |
71 | |
72 def image(req, id): | |
73 """Generate an image for the CAPTCHA with the given ID string""" | |
74 test = _getFactory(req).get(id) | |
75 if not test: | |
76 raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND | |
77 req.content_type = "image/jpeg" | |
78 test.render().save(req, "JPEG") | |
79 return apache.OK | |
80 | |
81 | |
82 def solution(req, id, word): | |
83 """Grade a CAPTCHA given a solution word""" | |
84 test = _getFactory(req).get(id) | |
85 if not test: | |
86 raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND | |
87 | |
88 if not test.valid: | |
89 # Invalid tests will always return False, to prevent | |
90 # random trial-and-error attacks. This could be confusing to a user... | |
91 result = "Test invalidated, try another test" | |
92 elif test.testSolutions([word]): | |
93 result = "Correct" | |
94 else: | |
95 result = "Incorrect" | |
96 | |
97 return """<html> | |
98 <head> | |
99 <title>PyCAPTCHA Example</title> | |
100 </head> | |
101 <body> | |
102 <h1>PyCAPTCHA Example</h1> | |
103 <h2>%s</h2> | |
104 <p><img src="image?id=%s"/></p> | |
105 <p><b>%s</b></p> | |
106 <p>You guessed: %s</p> | |
107 <p>Possible solutions: %s</p> | |
108 <p><a href="test">Try again</a></p> | |
109 </body> | |
110 </html> | |
111 """ % (test.__class__.__name__, test.id, result, word, ", ".join(test.solutions)) | |
112 | |
113 ### The End ### |