7.6. http_server — http server module API

7.6.1. Class http_server

http_server.simple_http_server_start(port, num_of_pages, html_pages)

Starts the http server.

  • port - the port number of the server
  • num_of_pages - the number of html pages that will be served
  • html_pages - tuple which contains 2 elements per html page <html name> <html code> with one or more pairs, it depends on the above num_of_pages (page1_html_name,page1_html_code , page2_html_name,page2_html_code,...)
    • page1_html_name - the name of the html page
    • page1_html_code - the html code of the html page
http_server.simple_http_server_stop()

Stop the http server

7.6.2. Example

# Example for the http_server module. The HTTP server supports up to 10 html pages.

import time
import wlan
import hardware
import http_server

#Initialize PORT
port=80

#Initialize the number of PAGES
num_of_pages=5

#Initialize PAGE 1
page1_html_name="/index.html"
page1_html_code="<html>HELLO <body style='background-color:green;'></body> </html>"

#Initialize PAGE 2
page2_html_name="/hello.html"
page2_html_code="<html> <head> <title> HTML TEST 2 </title> </head> <body  style='background-color:red;'><hr> <h1 style='color:green;'> Test!!! </h1> <h2 style='color:red;'> Test </h2> <hr><h3 style='color:orange;'> Test </h3> <h4 style='color:pink;'> Test </h4> <p> This is a paragraph </p> <p> Another paragraph </p> </p> <p> Another paragraph </p>  <p><a href='http://www.wubby.io'>This is a link</a></p><hr> <p style='text-align:center;'><a href='http://www.wubby.io'>WUBBY </a></p> </body> </html>"

#Initialize PAGE 3
page3_html_name="/page3.html"
page3_html_code="<html> <head> <title> HTML TEST 3 </title> </head> <body  style='background-color:pink;'> <h1 style='color:green;'> Test </h1> <h2 style='color:red;'> Test </h2> <h3 style='color:orange;'> Test </h3> <h4 style='color:blue;'> Test </h4> <p> This is a paragraph </p> <p> Another paragraph </p> </p> <p> Another paragraph </p>  <p><a href='http://www.wubby.io'>This is a link</a></p><hr> <p style='text-align:center;'><a href='http://www.wubby.io'>WUBBY </a></p> </body> </html>"

#Initialize PAGE 4
page4_html_name="/page4.html"
page4_html_code="<html> <head> <title> HTML TEST 5 </title> </head> <body  style='background-color:grey;'> <h1 style='color:blue;'> Test </h1> <h2 style='color:white;'> Test </h2> <h3 style='color:green;'> Test </h3> <h4 style='color:pink;'> Test </h4> <p> This is a paragraph </p> <p> Another paragraph </p> </p> <p> Another paragraph </p>  <p><a href='http://www.wubby.io'>This is a link</a></p><hr> <p style='text-align:center;'><a href='http://www.wubby.io'>WUBBY </a></p> </body> </html>"

info = hardware.info()

#Initialize PAGE 5
page5_html_name="/page5.html"
page5_html_code="<html> <head> <title> PLATFORM INFORMATIONS </title> </head> <body style='background-color:lightgrey;'> <hr> <p> Platform info : " + str(info) + "</p> <hr> <p> IP : " + wlan.ip() + "</p> <hr> </body> </html>"

#Insert in the tuple the information about pages with format <name>,<code>,...,<name><code>
html_pages = (page1_html_name,page1_html_code,page2_html_name,page2_html_code,page3_html_name,page3_html_code,page4_html_name,page4_html_code,page5_html_name,page5_html_code);

#START the HTTP server
http_server.simple_http_server_start(port,num_of_pages,html_pages)
print ("start SERVER at PORT: " + str(port))


#STOP the HTTP server
#http_server.simple_http_server_stop()
#print ("stop SERVER at PORT: " + str(port))

while(1):
        time.sleep(50)