Module:digiweb

From Digi Developer

Jump to: navigation, search

Provides an interface to allow the Digi web server to call into Python and allow Python code to handle web page requests.

The module contains a single type named Callback that is initialized with a callback function as follows:

Contents

Summary

digiweb.Callback(cb) -> handle

Returns a handle to be used for deregistration later. The callback will be handled as long as the callback handle exists. If the handle goes out of scope or is deallocated, the callback will be unregistered.

Callback functions should be of the form: > cb(type, path, headers, args) -> None | (type, data)

parameters

type
HTTP request type. Can be 'HEAD', 'GET' or 'POST'.
path
URL path component.
headers
A dictionary containing header information from the client. Contains host, agent and referer strings
args
Request Type specific
GET
URL-decoded form arguments in a dictonary
POST
Body of POST request in a single string
For either request type, If there is nothing provided in the request, None will be specified instead.

return values

  • None if this path or request type cannot be handled by the callback routine.
  • 2-tuple
    • type: For a successful request, determines the MIME type the server will report. Choose from TextHtml, TextPlain or TextXml.
    • data: Body of response content.

Limitations & Suggestions

  • Remember, call back functions will be executed in another thread. Shared data will need to be protected from race conditions due to concurrent access.
  • Do not try to use common page names like "index.htm", for while they may NOT exist within the Digi product, the Digi web server (as do all web servers) treat certain default pages as magic.
  • If you are creating a page name for users to remember, you may not want to require any extension at all, plus make them case insensitive. Telling users to use "192.168.1.1/status" is far easier than remembering if it is "status.htm" or "status.html" or "Status.HTML".
  • To add an image to your page, use this example HTML tag <img src="/FS/WEB/python/file.jpg" alt="device is running" />. The images must be uploaded to your Python directory by the same web ui page as for the Python code. Be very careful of the case, as 'Image.jpg' is not the same as 'image.jpg' or 'Image.JPG'. Also, your flash space is highly limited so keep image small and highly compressed. JPG at lower than normal compression seem the best. BMP are too large. PNG may be 'functionally too rich', so larger than required. GIF also tend to be larger than JPG unless you only have a few colors (line drawings etc).

Examples

The following code will serve any unrecognized path in the system with information on the request for one minute then exit.

import digiweb
 
template = """
<html><head><title>Request info</title></head>
<body>
%s request for path '%s'
<hr>
Headers: %s
<hr>
Args: %s
</body>
</html>
"""
 
def pageinfo(type, path, headers, args):
    return (digiweb.TextHtml, template % (type, path, headers, args))
 
if __name__ == "__main__":
    import time
    hnd = digiweb.Callback(pageinfo)
    time.sleep(60)


This example, like the one above will display request information, but it will make args be a dict object instead of a str during a POST request so it can be treated the same way as a GET request.

import digiweb
import cgi
 
template = """
<html><head><title>Request info</title></head>
<body>
%s request for path '%s'
<hr>
Headers: %s
<hr>
Args: %s
</body>
</html>
"""
 
def pageinfo(type, path, headers, args):
    tmp = {}
    if type=="POST":
      tmp = cgi.parse_qs(args)
      args=tmp
      for key in args:
        args[key] = args[key][0]
    return (digiweb.TextHtml, template % (type, path, headers, args))
 
if __name__ == "__main__":
    import time
    hnd = digiweb.Callback(pageinfo)
    time.sleep(60)

Availability

This feature is available as of firmware revision 2.8.0.

Personal tools
Wiki Editing