Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
bf258c7e
Commit
bf258c7e
authored
Aug 29, 2010
by
valenok
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored python bindings according to the API change
parent
6cf29651
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
24 deletions
+26
-24
example.py
bindings/python/example.py
+12
-13
mongoose.py
bindings/python/mongoose.py
+14
-11
No files found.
bindings/python/example.py
View file @
bf258c7e
...
@@ -8,8 +8,13 @@ import mongoose
...
@@ -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):
...
@@ -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):
...
@@ -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
...
...
bindings/python/mongoose.py
View file @
bf258c7e
...
@@ -40,6 +40,12 @@ import ctypes
...
@@ -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):
...
@@ -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):
...
@@ -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):
...
@@ -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):
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment