Simple Python Web server

How to set up a simple, temporary Web server on your laptop or embedded device for a quick demo and make it accessible from anywhere.

This app note works for Arduino Yun, BeagleBone, Mac OS X, Raspberry Pi and other Linux devices running Python. Found a bug? Let us know.

Creating a minimal HTML Web page (optional)

  1. Open a shell on your device, or type
    $ cd ~
  2. Create a python-web-server directory
    $ mkdir python-web-server
    $ cd python-web-server
  3. Create a minimal index.html Web page with
    $ echo "<html><body>Hello</body></html>" > index.html

Running a simple Python server on your device like Gary Robinson

  1. Start the built in Python Web server with
    $ python -m SimpleHTTPServer 8080
    Or, if that does not work, try the Python 3.x version
    $ python -m http.server 8080
    Note that this exposes your current directory and everything below it.

Accessing the simple Python Web server locally

  1. To access the Web server, visit the URL
    http://LOCAL_IP:8080/
    e.g. for the local IP address 192.168.0.23 go to
    http://192.168.0.23:8080/ or http://127.0.0.1:8080/ on same device

Setting up remote Web access to your device

  1. Install YalerTunnel and enable Web access via Yaler
    Install YalerTunnel on Arduino Yun, BeagleBone, Mac, Raspberry Pi or another device
  2. Make sure to set the local port to 8080 and use your relay domain
    $ ./yalertunnel server 127.0.0.1:8080 try.yaler.io:80 RELAY_DOMAIN
    Or, to enable TLS
    $ ./yalertunnel server 127.0.0.1:8080 ssl:try.yaler.io:443 RELAY_DOMAIN

Accessing the simple Python Web server remotely

  1. To access the Web service, visit the URL
    http://RELAY_DOMAIN.try.yaler.io/ or https://RELAY_DOMAIN.try.yaler.io/ for TLS
    e.g. for the relay domain gsiot-ffmq-ttd5 go to
    http://gsiot-ffmq-ttd5.try.yaler.io/ or https://gsiot-ffmq-ttd5.try.yaler.io/ for TLS
  2. Done. You should now see a Web page served by your device.


Basic Auth Python Web server

How to set up a Python Web server with basic access authentication.

Creating a Basic Auth Python Web server like Sun Junyi

  1. Create a file named BasicAuthServer.py, e.g. with
    $ cd ~/python-web-server
    $ sudo nano BasicAuthServer.py
    Enter the following Python code
    ''' (c) Sun Junyi, https://gist.github.com/fxsjy/5465353 '''
    import BaseHTTPServer
    from SimpleHTTPServer import SimpleHTTPRequestHandler
    import sys
    import base64
    
    key = ""
    
    class AuthHandler(SimpleHTTPRequestHandler):
        def do_HEAD(self):
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
    
        def do_AUTHHEAD(self):
            self.send_response(401)
            self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
            self.send_header('Content-type', 'text/html')
            self.end_headers()
    
        def do_GET(self):
            global key
            if self.headers.getheader('Authorization') == None:
                self.do_AUTHHEAD()
                self.wfile.write('no auth header received')
            elif self.headers.getheader('Authorization') == 'Basic ' + key:
                SimpleHTTPRequestHandler.do_GET(self)
            else:
                self.do_AUTHHEAD()
                self.wfile.write(self.headers.getheader('Authorization'))
                self.wfile.write('not authenticated')
    
    def test(HandlerClass = AuthHandler,
             ServerClass = BaseHTTPServer.HTTPServer):
        BaseHTTPServer.test(HandlerClass, ServerClass)
    
    if __name__ == '__main__':
        if len(sys.argv) < 3:
            print "usage BasicAuthServer.py [port] [username:password]"
            sys.exit()
        key = base64.b64encode(sys.argv[2])
        test()

    Save changes with CTRL-X, then Y, then RETURN.

  2. Start the Python Web server with
    $ python BasicAuthServer.py 8080 USERNAME:PASSWORD

Accessing the Basic Auth Python Web server remotely

Assuming you already enabled remote Web access with TLS as shown above.

  1. To access the Web server, visit the URL
    https://RELAY_DOMAIN.try.yaler.io/
    e.g. for the relay domain gsiot-ffmq-ttd5 go to
    https://gsiot-ffmq-ttd5.try.yaler.io/
  2. Done. You should now see a login prompt to the Web page served by your device.


Troubleshooting

How to fix common issues.


Creative Commons License This work by Yaler GmbH is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.