File: http_example.py

package info (click to toggle)
pycaptcha 0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,224 kB
  • sloc: python: 699; makefile: 8; sh: 7
file content (154 lines) | stat: -rwxr-xr-x 4,248 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
#
# An example that presents CAPTCHA tests in a web environment
# and gives the user a chance to solve them. Run it, optionally
# specifying a port number on the command line, then point your web
# browser at the given URL.
#

from Captcha.Visual import Tests
from Captcha import Factory
import BaseHTTPServer, urlparse, sys


class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        scheme, host, path, parameters, query, fragment = urlparse.urlparse(self.path)

        # Split the path into segments
        pathSegments = path.split('/')[1:]

        # Split the query into key-value pairs
        args = {}
        for pair in query.split("&"):
            if pair.find("=") >= 0:
                key, value = pair.split("=", 1)
                args.setdefault(key, []).append(value)
            else:
                args[pair] = []

        # A hack so it works with a proxy configured for VHostMonster :)
        if pathSegments[0] == "vhost":
            pathSegments = pathSegments[3:]

        if pathSegments[0] == "":
            self.handleRootPage(args.get('test', Tests.__all__)[0])

        elif pathSegments[0] == "images":
            self.handleImagePage(pathSegments[1])

        elif pathSegments[0] == "solutions":
            self.handleSolutionPage(pathSegments[1], args['word'][0])

        else:
            self.handle404()

    def handle404(self):
        self.send_response(404)
        self.send_header("Content-Type", "text/html")
        self.end_headers()
        self.wfile.write("<html><body><h1>No such resource</h1></body></html>")

    def handleRootPage(self, testName):
        self.send_response(200)
        self.send_header("Content-Type", "text/html")
        self.end_headers()

        test = self.captchaFactory.new(getattr(Tests, testName))

        # Make a list of tests other than the one we're using
        others = []
        for t in Tests.__all__:
            if t != testName:
                others.append('<li><a href="/?test=%s">%s</a></li>' % (t,t))
        others = "\n".join(others)

        self.wfile.write("""<html>
<head>
<title>PyCAPTCHA Example</title>
</head>
<body>
<h1>PyCAPTCHA Example</h1>
<p>
  <b>%s</b>:
  %s
</p>

<p><img src="/images/%s"/></p>
<p>
  <form action="/solutions/%s" method="get">
    Enter the word shown:
    <input type="text" name="word"/>
  </form>
</p>

<p>
Or try...
<ul>
%s
</ul>
</p>

</body>
</html>
""" % (test.__class__.__name__, test.__doc__, test.id, test.id, others))

    def handleImagePage(self, id):
        test = self.captchaFactory.get(id)
        if not test:
            return self.handle404()

        self.send_response(200)
        self.send_header("Content-Type", "image/jpeg")
        self.end_headers()
        test.render().save(self.wfile, "JPEG")

    def handleSolutionPage(self, id, word):
        test = self.captchaFactory.get(id)
        if not test:
            return self.handle404()

        if not test.valid:
            # Invalid tests will always return False, to prevent
            # random trial-and-error attacks. This could be confusing to a user...
            result = "Test invalidated, try another test"
        elif test.testSolutions([word]):
            result = "Correct"
        else:
            result = "Incorrect"

        self.send_response(200)
        self.send_header("Content-Type", "text/html")
        self.end_headers()
        self.wfile.write("""<html>
<head>
<title>PyCAPTCHA Example</title>
</head>
<body>
<h1>PyCAPTCHA Example</h1>
<h2>%s</h2>
<p><img src="/images/%s"/></p>
<p><b>%s</b></p>
<p>You guessed: %s</p>
<p>Possible solutions: %s</p>
<p><a href="/">Try again</a></p>
</body>
</html>
""" % (test.__class__.__name__, test.id, result, word, ", ".join(test.solutions)))


def main(port):
    print "Starting server at http://localhost:%d/" % port
    handler = RequestHandler
    handler.captchaFactory = Factory()
    BaseHTTPServer.HTTPServer(('', port), RequestHandler).serve_forever()

if __name__ == "__main__":
    # The port number can be specified on the command line, default is 8080
    if len(sys.argv) >= 2:
        port = int(sys.argv[1])
    else:
        port = 8080
    main(port)

### The End ###