mirror of
https://github.com/lammertb/libhttp.git
synced 2025-08-22 08:12:46 +03:00
refactored python bindings according to the API change
This commit is contained in:
@@ -8,8 +8,13 @@ import mongoose
|
||||
import sys
|
||||
|
||||
# Handle /show and /form URIs.
|
||||
def EventHandler(conn, info):
|
||||
if info.uri == '/show':
|
||||
def EventHandler(event, conn, info):
|
||||
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', 'Content-Type: text/plain\r\n\r\n')
|
||||
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))
|
||||
my_var = conn.get_var(post_data, 'my_var')
|
||||
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('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':
|
||||
return True
|
||||
elif event == mongoose.NEW_REQUEST and 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>'
|
||||
@@ -33,16 +38,10 @@ def EventHandler(conn, info):
|
||||
'<input type="text" name="my_var"/>'
|
||||
'<input type="submit"/>'
|
||||
'</form>')
|
||||
return mongoose.MG_SUCCESS
|
||||
return True
|
||||
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
|
||||
# List of options may be specified in the contructor
|
||||
|
Reference in New Issue
Block a user