summaryrefslogtreecommitdiffstats
path: root/mongoose/bindings/python/example.py
blob: 9c3f83ef28660bda43bdb95e9c0f7604ac1cd7f9 (plain)
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
# This is Python example on how to use Mongoose embeddable web server,
# http://code.google.com/p/mongoose
#
# Before using the mongoose module, make sure that Mongoose shared library is
# built and present in the current (or system library) directory

import mongoose
import sys

# Handle /show and /form URIs.
def EventHandler(event, conn, info):
    if event == mongoose.HTTP_ERROR:
        conn.printf('%s', 'HTTP/1.0 200 OK\r\n')
        conn.printf('%s', 'Content-Type: text/plain\r\n\r\n')
        conn.printf('HTTP error: %d\n', info.status_code)
        return True
    elif event == mongoose.NEW_REQUEST and info.uri == '/show':
        conn.printf('%s', 'HTTP/1.0 200 OK\r\n')
        conn.printf('%s', 'Content-Type: text/plain\r\n\r\n')
        conn.printf('%s %s\n', info.request_method, info.uri)
        if info.request_method == 'POST':
            content_len = conn.get_header('Content-Length')
            post_data = conn.read(int(content_len))
            my_var = conn.get_var(post_data, 'my_var')
        else:
            my_var = conn.get_var(info.query_string, 'my_var')
        conn.printf('my_var: %s\n', my_var or '<not set>')
        conn.printf('HEADERS: \n')
        for header in info.http_headers[:info.num_headers]:
            conn.printf('  %s: %s\n', header.name, header.value)
        return True
    elif event == mongoose.NEW_REQUEST and info.uri == '/form':
        conn.write('HTTP/1.0 200 OK\r\n'
               'Content-Type: text/html\r\n\r\n'
               'Use GET: <a href="/show?my_var=hello">link</a>'
               '<form action="/show" method="POST">'
               'Use POST: type text and submit: '
               '<input type="text" name="my_var"/>'
               '<input type="submit"/>'
               '</form>')
        return True
    elif event == mongoose.NEW_REQUEST and info.uri == '/secret':
        conn.send_file('/etc/passwd')
        return True
    else:
        return False


# Create mongoose object, and register '/foo' URI handler
# List of options may be specified in the contructor
server = mongoose.Mongoose(EventHandler,
                           document_root='/tmp',
                           listening_ports='8080')

print ('Mongoose started on port %s, press enter to quit'
       % server.get_option('listening_ports'))

sys.stdin.read(1)

# Deleting server object stops all serving threads
print 'Stopping server.'
del server