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.
$ cd ~
$ mkdir python-web-server $ cd python-web-server
$ echo "<html><body>Hello</body></html>" > index.html
$ 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.
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
Install YalerTunnel on Arduino Yun, BeagleBone, Mac, Raspberry Pi or another device
$ ./yalertunnel server 127.0.0.1:8080 try.yaler.io:80 RELAY_DOMAINOr, to enable TLS
$ ./yalertunnel server 127.0.0.1:8080 ssl:try.yaler.io:443 RELAY_DOMAIN
http://RELAY_DOMAIN.try.yaler.io/ or https://RELAY_DOMAIN.try.yaler.io/ for TLSe.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
How to set up a Python Web server with basic access authentication.
$ cd ~/python-web-server $ sudo nano BasicAuthServer.pyEnter 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.
$ python BasicAuthServer.py 8080 USERNAME:PASSWORD
Assuming you already enabled remote Web access with TLS as shown above.
https://RELAY_DOMAIN.try.yaler.io/e.g. for the relay domain gsiot-ffmq-ttd5 go to
https://gsiot-ffmq-ttd5.try.yaler.io/
How to fix common issues.
command not found
download and install Python from https://www.python.org/Address already in use
start the server with another port, e.g. 8081This work by Yaler GmbH is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.