Commit ef04ce98 authored by valenok's avatar valenok

enhance python binding - add example for GET/POST, and use mg_read() there.

parent e8462121
...@@ -7,24 +7,37 @@ ...@@ -7,24 +7,37 @@
import mongoose import mongoose
import sys import sys
# This function is a "/foo" URI handler: it will be called each time # Handle /show and /form URIs.
# HTTP request to http://this_machine:8080/foo made.
# It displays some request information.
def uri_handler(conn, info): def uri_handler(conn, info):
if info.uri != '/foo': if 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_qsvar(info, '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 mongoose.MG_SUCCESS
elif 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 mongoose.MG_SUCCESS
else:
return mongoose.MG_ERROR return mongoose.MG_ERROR
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)
conn.printf('my_var: %s\n',
conn.get_qsvar(info, '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 mongoose.MG_SUCCESS
# This function is 404 error handler: it is called each time requested # Invoked each time HTTP error is triggered.
# document is not found by the server.
def error_handler(conn, info): def error_handler(conn, info):
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')
......
...@@ -122,16 +122,15 @@ class Connection(object): ...@@ -122,16 +122,15 @@ class Connection(object):
val = self.m.dll.mg_get_header(self.conn, name) val = self.m.dll.mg_get_header(self.conn, name)
return ctypes.c_char_p(val).value return ctypes.c_char_p(val).value
def get_var(self, buf, buflen, name): def get_var(self, data, name):
size = 1024 size = len(data)
value = ctypes.create_string_buffer(size) buf = ctypes.create_string_buffer(size)
self.m.dll.mg_get_var.restype = ctypes.c_int n = self.m.dll.mg_get_var(data, size, name, buf, size)
result = self.m.dll.mg_get_var(buf, buflen, name, value, size) return n == MG_SUCCESS and buf or None
return result == MG_ERROR and None or value
def get_qsvar(self, request_info, name): def get_qsvar(self, request_info, name):
qs = request_info.query_string qs = request_info.query_string
return qs and self.get_var(qs, len(qs), name) or None return qs and self.get_var(qs, name) 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)
...@@ -140,6 +139,12 @@ class Connection(object): ...@@ -140,6 +139,12 @@ class Connection(object):
def write(self, data): def write(self, data):
val = self.m.dll.mg_write(self.conn, data, len(data)) val = self.m.dll.mg_write(self.conn, data, len(data))
return ctypes.c_int(val).value return ctypes.c_int(val).value
def read(self, size):
buf = ctypes.create_string_buffer(size)
n = self.m.dll.mg_read(self.conn, buf, size)
print size, buf, n
return n <= 0 and None or buf[:n]
class Mongoose(object): class Mongoose(object):
...@@ -148,6 +153,17 @@ class Mongoose(object): ...@@ -148,6 +153,17 @@ class Mongoose(object):
def __init__(self, **kwargs): def __init__(self, **kwargs):
dll_extension = os.name == 'nt' and 'dll' or 'so' dll_extension = os.name == 'nt' and 'dll' or 'so'
self.dll = ctypes.CDLL('_mongoose.%s' % dll_extension) self.dll = ctypes.CDLL('_mongoose.%s' % dll_extension)
self.dll.mg_start.restype = ctypes.c_void_p
self.dll.mg_modify_passwords_file.restype = ctypes.c_int
self.dll.mg_read.restype = ctypes.c_int
self.dll.mg_write.restype = ctypes.c_int
self.dll.mg_printf.restype = ctypes.c_int
self.dll.mg_get_header.restype = ctypes.c_char_p
self.dll.mg_get_var.restype = ctypes.c_int
self.dll.mg_get_qsvar.restype = ctypes.c_int
self.dll.mg_get_cookie.restype = ctypes.c_int
self.callbacks = [] self.callbacks = []
self.config = mg_config(num_threads='5', self.config = mg_config(num_threads='5',
enable_directory_listing='yes', enable_directory_listing='yes',
...@@ -163,7 +179,6 @@ class Mongoose(object): ...@@ -163,7 +179,6 @@ class Mongoose(object):
setattr(self.config, key, cb) setattr(self.config, key, cb)
else: else:
setattr(self.config, key, str(value)) setattr(self.config, key, str(value))
self.dll.mg_start.restype = ctypes.c_void_p
self.ctx = self.dll.mg_start(ctypes.byref(self.config)) self.ctx = self.dll.mg_start(ctypes.byref(self.config))
def __del__(self): def __del__(self):
...@@ -178,7 +193,8 @@ class Mongoose(object): ...@@ -178,7 +193,8 @@ class Mongoose(object):
# 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)
return python_func(conn, request_info.contents) status = python_func(conn, request_info.contents)
return status == MG_SUCCESS and MG_SUCCESS or MG_ERROR
# Convert the closure into C callable object # Convert the closure into C callable object
c_func = mg_callback_t(func) c_func = mg_callback_t(func)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment