def main(): # Create mongoose object, and register '/foo' URI handler # List of options may be specified in the contructor handler = mongoose_wsgi.WSGIEventHandler('localhost', 8080, simple_app) server = mongoose.Mongoose( handler, document_root='/tmp', listening_ports='8080', num_threads='2', ) 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
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
conn.printf('HEADERS: \n') for header in info.http_headers[:info.num_headers]: conn.printf(' %s: %s\n', header.name, header.value) # This function is 404 error handler: it is called each time requested # document is not found by the server. def error_404_handler(conn, info, data): conn.printf('%s', 'HTTP/1.0 200 OK\r\n') conn.printf('%s', 'Content-Type: text/plain\r\n\r\n') conn.printf('Document %s not found!\n', info.uri) # Create mongoose object, and register '/foo' URI handler # List of options may be specified in the contructor server = mongoose.Mongoose(root='/tmp', ports='8080') # Register custom URI and 404 error handler server.bind_to_uri('/foo', uri_handler, 0) server.bind_to_error_code(404, error_404_handler, 0) # Any option may be set later on by setting an attribute of the server object server.ports = '8080,8081' # Listen on port 8081 in addition to 8080 # Mongoose options can be retrieved by asking an attribute print 'Starting Mongoose %s on port(s) %s ' % (server.version, server.ports) print 'CGI extensions: %s' % server.cgi_ext # Serve connections for 5 minutes and then exit time.sleep(5 * 60)