refactored python bindings according to the API change

This commit is contained in:
valenok 2010-08-29 23:58:56 +01:00
parent 6cf296515b
commit bf258c7ee1
2 changed files with 26 additions and 24 deletions

View File

@ -8,8 +8,13 @@ import mongoose
import sys import sys
# Handle /show and /form URIs. # Handle /show and /form URIs.
def EventHandler(conn, info): def EventHandler(event, conn, info):
if info.uri == '/show': 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', 'HTTP/1.0 200 OK\r\n')
conn.printf('%s', 'Content-Type: text/plain\r\n\r\n') conn.printf('%s', 'Content-Type: text/plain\r\n\r\n')
conn.printf('%s %s\n', info.request_method, info.uri) conn.printf('%s %s\n', info.request_method, info.uri)
@ -18,13 +23,13 @@ def EventHandler(conn, info):
post_data = conn.read(int(content_len)) post_data = conn.read(int(content_len))
my_var = conn.get_var(post_data, 'my_var') my_var = conn.get_var(post_data, 'my_var')
else: else:
my_var = conn.get_qsvar(info, 'my_var') my_var = conn.get_var(info.query_string, 'my_var')
conn.printf('my_var: %s\n', my_var or '<not set>') conn.printf('my_var: %s\n', my_var or '<not set>')
conn.printf('HEADERS: \n') conn.printf('HEADERS: \n')
for header in info.http_headers[:info.num_headers]: for header in info.http_headers[:info.num_headers]:
conn.printf(' %s: %s\n', header.name, header.value) conn.printf(' %s: %s\n', header.name, header.value)
return mongoose.MG_SUCCESS return True
elif info.uri == '/form': elif event == mongoose.NEW_REQUEST and info.uri == '/form':
conn.write('HTTP/1.0 200 OK\r\n' conn.write('HTTP/1.0 200 OK\r\n'
'Content-Type: text/html\r\n\r\n' 'Content-Type: text/html\r\n\r\n'
'Use GET: <a href="/show?my_var=hello">link</a>' 'Use GET: <a href="/show?my_var=hello">link</a>'
@ -33,16 +38,10 @@ def EventHandler(conn, info):
'<input type="text" name="my_var"/>' '<input type="text" name="my_var"/>'
'<input type="submit"/>' '<input type="submit"/>'
'</form>') '</form>')
return mongoose.MG_SUCCESS return True
else: else:
return mongoose.MG_ERROR return False
# Invoked each time HTTP error is triggered.
def error_handler(conn, info):
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 mongoose.MG_SUCCESS
# Create mongoose object, and register '/foo' URI handler # Create mongoose object, and register '/foo' URI handler
# List of options may be specified in the contructor # List of options may be specified in the contructor

View File

@ -40,6 +40,12 @@ import ctypes
import os import os
NEW_REQUEST = 0
HTTP_ERROR = 1
EVENT_LOG = 2
INIT_SSL = 3
class mg_header(ctypes.Structure): class mg_header(ctypes.Structure):
"""A wrapper for struct mg_header.""" """A wrapper for struct mg_header."""
_fields_ = [ _fields_ = [
@ -67,6 +73,7 @@ class mg_request_info(ctypes.Structure):
mg_callback_t = ctypes.CFUNCTYPE(ctypes.c_void_p, mg_callback_t = ctypes.CFUNCTYPE(ctypes.c_void_p,
ctypes.c_int,
ctypes.c_void_p, ctypes.c_void_p,
ctypes.POINTER(mg_request_info)) ctypes.POINTER(mg_request_info))
@ -87,7 +94,7 @@ class Connection(object):
size = len(data) size = len(data)
buf = ctypes.create_string_buffer(size) buf = ctypes.create_string_buffer(size)
n = self.m.dll.mg_get_var(data, size, name, buf, size) n = self.m.dll.mg_get_var(data, size, name, buf, size)
return n == MG_SUCCESS and buf or None return n >= 0 and buf or None
def printf(self, fmt, *args): def printf(self, fmt, *args):
val = self.m.dll.mg_printf(self.conn, fmt, *args) val = self.m.dll.mg_printf(self.conn, fmt, *args)
@ -100,7 +107,6 @@ class Connection(object):
def read(self, size): def read(self, size):
buf = ctypes.create_string_buffer(size) buf = ctypes.create_string_buffer(size)
n = self.m.dll.mg_read(self.conn, buf, size) n = self.m.dll.mg_read(self.conn, buf, size)
print size, buf, n
return n <= 0 and None or buf[:n] return n <= 0 and None or buf[:n]
@ -123,30 +129,27 @@ class Mongoose(object):
if callback: if callback:
# Create a closure that will be called by the shared library. # Create a closure that will be called by the shared library.
def func(connection, request_info): def func(event, connection, request_info):
# Wrap connection pointer into the connection # Wrap connection pointer into the connection
# object and call Python callback # object and call Python callback
conn = Connection(self, connection) conn = Connection(self, connection)
if python_func(conn, request_info.contents): return callback(event, conn, request_info.contents) and 1 or 0
return 'non-null-pointer'
else:
return ctypes.c_void_p(0)
# Convert the closure into C callable object # Convert the closure into C callable object
self.callback = mg_callback_t(func) self.callback = mg_callback_t(func)
self.callback.restype = ctypes.c_void_p self.callback.restype = ctypes.c_char_p
else: else:
self.callback = ctypes.c_void_p(0) self.callback = ctypes.c_void_p(0)
args = [y for x in kwargs.items() for y in x] + [None] args = [y for x in kwargs.items() for y in x] + [None]
options = (ctypes.c_char_p * len(args))(*args) options = (ctypes.c_char_p * len(args))(*args)
# self.ctx = self.dll.mg_start(self.callback, options) ret = self.dll.mg_start(self.callback, options)
self.ctx = self.dll.mg_start(ctypes.c_void_p(0), options) self.ctx = ctypes.c_void_p(ret)
def __del__(self): def __del__(self):
"""Destructor, stop Mongoose instance.""" """Destructor, stop Mongoose instance."""
self.dll.mg_stop(ctypes.c_void_p(self.ctx)) self.dll.mg_stop(self.ctx)
def get_option(self, name): def get_option(self, name):
return self.dll.mg_get_option(self.ctx, name) return self.dll.mg_get_option(self.ctx, name)