Write a simple HTTP server in Python

The default Python distribution has a built-in support to the HTTP protocol that you can use to make a simple stand-alone Web server.

The Python module that provides this support is called BaseFTTPServer and can be used in our programs just including it in our sources:

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

Hello world !

Let's start with the first basic example. It just return "Hello World !" on the web browser.

example1.py

Run it by typing:

debarm:~/playground/python/httpserver# python example1.py

Then open your web browser on this URL: http://fox_ip_address:8080.

"Hello World !" will appear on your browser and two log messages on the FOX console:

Started httpserver on port  8080
10.55.98.7 - - [30/Jan/2012 15:40:52] "GET / HTTP/1.1" 200 -
10.55.98.7 - - [30/Jan/2012 15:40:52] "GET /favicon.ico HTTP/1.1" 200 -

Serve static files

Let's try an example that shows how to serve static files like index.html, style.css, javascript code and images:

example2.py

This new example:

  • check the extension of the file requested file
  • set the right mime type to give back to the browser
  • open the static file requested
  • send it back to the browser

Run it by typing:

debarm:~/playground/python/httpserver# python example2.py

then open your web browser on this URL: http://fox_ip_address:8080.

An HTML home page will appear on your browser.

Read data from a form

Now explore how to read incoming data from an HTML form.

example3.py

Run this example:

debarm:~/playground/python/httpserver# python example3.py

and type your name in the "Your name" label.

Related links

About the author:
Sergio Tanzilli - tanzilli@acmesystems.it
http://www.tanzilli.com - https://github.com/tanzilli

ARIETTA-G25
EUR 25.00


ARM9 Linux Embedded Module (128MB)

Arietta G25 is a small and low-cost multi-chip module that integrates:

  • a ARM9 @ 400Mhz CPU Atmel AT91SAM9G25
  • a 128MB of DDR2 RAM
  • up to 3 USB 2.0 host ports
  • 20x2 pads for strip pitch 2.54mm (100mil)

Basic Version with no strips mounted. If you need to have any strip mounted take a look to the Welding services

| Product description |

COMBO-B43
EUR 149.00


Acqua A5 + 4.3 inch TFT LCD evaluation kit

Basic kit to evaluate the Acqua A25 SoM with TFT LCD 4.3 inch

Parts included:

  • ACQUA-256E-N256 SoM (256 MB RAM , 256 MB Nandflash)
  • 4.3 inch TFT LCD with resistive touch (Code LCD-43-RT)
  • Berta board A5 LCD (Code BERTA-A5-LCD)
  • WIFI-2 WiFi module with PCB antenna
  • A bootable microSD with Debian Linux preinstalled
  • Debug port interface (Code DPI) with USB data cable
  • Wall adapter 5 volt @ 1A switching power supply (Code PS-5V1A-EU-MUSB)

| Product description | Acqua A5 catalog |