annotate data_generation/transformations/pycaptcha/modpython_example.py @ 287:f9b93ae45723

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