annotate data_generation/transformations/pycaptcha/http_example.py @ 312:bd6085d77706

Avoir exactement le meme jeu de donnees pour pre-train et finetune
author SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
date Thu, 01 Apr 2010 14:25:40 -0400
parents 1f5937e9e530
children
rev   line source
87
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
1 #!/usr/bin/env python
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
2 #
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
3 # An example that presents CAPTCHA tests in a web environment
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
4 # and gives the user a chance to solve them. Run it, optionally
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
5 # specifying a port number on the command line, then point your web
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
6 # browser at the given URL.
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
7 #
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
8
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
9 from Captcha.Visual import Tests
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
10 from Captcha import Factory
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
11 import BaseHTTPServer, urlparse, sys
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
12
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
13
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
14 class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
15 def do_GET(self):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
16 scheme, host, path, parameters, query, fragment = urlparse.urlparse(self.path)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
17
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
18 # Split the path into segments
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
19 pathSegments = path.split('/')[1:]
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
20
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
21 # Split the query into key-value pairs
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
22 args = {}
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
23 for pair in query.split("&"):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
24 if pair.find("=") >= 0:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
25 key, value = pair.split("=", 1)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
26 args.setdefault(key, []).append(value)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
27 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
28 args[pair] = []
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
29
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
30 # A hack so it works with a proxy configured for VHostMonster :)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
31 if pathSegments[0] == "vhost":
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
32 pathSegments = pathSegments[3:]
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
33
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
34 if pathSegments[0] == "":
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
35 self.handleRootPage(args.get('test', Tests.__all__)[0])
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
36
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
37 elif pathSegments[0] == "images":
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
38 self.handleImagePage(pathSegments[1])
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
39
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
40 elif pathSegments[0] == "solutions":
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
41 self.handleSolutionPage(pathSegments[1], args['word'][0])
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
42
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
43 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
44 self.handle404()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
45
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
46 def handle404(self):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
47 self.send_response(404)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
48 self.send_header("Content-Type", "text/html")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
49 self.end_headers()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
50 self.wfile.write("<html><body><h1>No such resource</h1></body></html>")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
51
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
52 def handleRootPage(self, testName):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
53 self.send_response(200)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
54 self.send_header("Content-Type", "text/html")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
55 self.end_headers()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
56
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
57 test = self.captchaFactory.new(getattr(Tests, testName))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
58
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
59 # Make a list of tests other than the one we're using
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
60 others = []
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
61 for t in Tests.__all__:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
62 if t != testName:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
63 others.append('<li><a href="/?test=%s">%s</a></li>' % (t,t))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
64 others = "\n".join(others)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
65
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
66 self.wfile.write("""<html>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
67 <head>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
68 <title>PyCAPTCHA Example</title>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
69 </head>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
70 <body>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
71 <h1>PyCAPTCHA Example</h1>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
72 <p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
73 <b>%s</b>:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
74 %s
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
75 </p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
76
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
77 <p><img src="/images/%s"/></p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
78 <p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
79 <form action="/solutions/%s" method="get">
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
80 Enter the word shown:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
81 <input type="text" name="word"/>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
82 </form>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
83 </p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
84
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
85 <p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
86 Or try...
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
87 <ul>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
88 %s
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
89 </ul>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
90 </p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
91
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
92 </body>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
93 </html>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
94 """ % (test.__class__.__name__, test.__doc__, test.id, test.id, others))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
95
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
96 def handleImagePage(self, id):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
97 test = self.captchaFactory.get(id)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
98 if not test:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
99 return self.handle404()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
100
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
101 self.send_response(200)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
102 self.send_header("Content-Type", "image/jpeg")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
103 self.end_headers()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
104 test.render().save(self.wfile, "JPEG")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
105
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
106 def handleSolutionPage(self, id, word):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
107 test = self.captchaFactory.get(id)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
108 if not test:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
109 return self.handle404()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
110
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
111 if not test.valid:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
112 # Invalid tests will always return False, to prevent
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
113 # random trial-and-error attacks. This could be confusing to a user...
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
114 result = "Test invalidated, try another test"
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
115 elif test.testSolutions([word]):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
116 result = "Correct"
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
117 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
118 result = "Incorrect"
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
119
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
120 self.send_response(200)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
121 self.send_header("Content-Type", "text/html")
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
122 self.end_headers()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
123 self.wfile.write("""<html>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
124 <head>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
125 <title>PyCAPTCHA Example</title>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
126 </head>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
127 <body>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
128 <h1>PyCAPTCHA Example</h1>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
129 <h2>%s</h2>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
130 <p><img src="/images/%s"/></p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
131 <p><b>%s</b></p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
132 <p>You guessed: %s</p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
133 <p>Possible solutions: %s</p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
134 <p><a href="/">Try again</a></p>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
135 </body>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
136 </html>
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
137 """ % (test.__class__.__name__, test.id, result, word, ", ".join(test.solutions)))
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
138
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
139
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
140 def main(port):
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
141 print "Starting server at http://localhost:%d/" % port
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
142 handler = RequestHandler
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
143 handler.captchaFactory = Factory()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
144 BaseHTTPServer.HTTPServer(('', port), RequestHandler).serve_forever()
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
145
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
146 if __name__ == "__main__":
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
147 # The port number can be specified on the command line, default is 8080
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
148 if len(sys.argv) >= 2:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
149 port = int(sys.argv[1])
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
150 else:
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
151 port = 8080
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
152 main(port)
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
153
4775b4195b4b code pour la generation de captchas
goldfinger
parents:
diff changeset
154 ### The End ###