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
ef04ce98
Commit
ef04ce98
authored
Aug 27, 2010
by
valenok
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enhance python binding - add example for GET/POST, and use mg_read() there.
parent
e8462121
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
24 deletions
+53
-24
example.py
bindings/python/example.py
+28
-15
mongoose.py
bindings/python/mongoose.py
+25
-9
No files found.
bindings/python/example.py
View file @
ef04ce98
...
@@ -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'
:
return
mongoose
.
MG_ERROR
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
)
conn
.
printf
(
'my_var:
%
s
\n
'
,
if
info
.
request_method
==
'POST'
:
conn
.
get_qsvar
(
info
,
'my_var'
)
or
'<not set>'
)
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
'
)
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
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
# 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
'
)
...
...
bindings/python/mongoose.py
View file @
ef04ce98
...
@@ -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
)
...
@@ -141,6 +140,12 @@ class Connection(object):
...
@@ -141,6 +140,12 @@ class Connection(object):
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
):
"""A wrapper class for Mongoose shared library."""
"""A wrapper class for Mongoose shared library."""
...
@@ -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
)
...
...
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