Python's http.server

Posted on Dec 20, 2023

Now you might want to hack something together to run in the browser – I have this Fréchet distance visualisation, for example, that I wanted to run on a tablet and this seemed to be the easiest way to go.

But then if you’re developing this locally, your JavaScript can’t read any files because, like, this is javascript in the browser and it shouldn’t be reading your files. And even if the files are on a server somewhere you get into cross site scripting shenanigans, so you really kind of need to serve things over HTTP for real.

And that’s my advice for today: if you have a Python installation, you have a simple web server. You can just run

python -m http.server

and then a port number if you want.

This starts serving the current directory on the specified port and you access it from the browser at localhost : port number. Now you can test your stuff locally and I guess you could even use it for makeshift file transfer on your local network, but you should probably look up the details before you hook this up to the internet. Be safe out there.