Ever felt the need to have a local server set up so that you can test your webpages or just outright share some stuff(on same wifi that is).

This can be easily achieved by any computer that has python on it as it comes with its own server which can be accessed by the following command:

python -m http.server 1234

Here 1234 is the port number which can be any open port of your choice.

You can access the server via your own IP followed by port like http://192.168.137.35:1234/. You get your IP by using ip a on linux and ipconfig on windows.

ip

But I wanted a oneline solution that can easily start a server in the current directory and also show a QR according to IP as when on DHCP sometimes the local IP changes.

So its time to lay some PIPES now o_O

Getting the IP

You can get IP from ip a but that gives too much info, another tool could be used to get it directly but I’d prefer one regex pattern over 100 random tools. So we use the good old grep command with pattern matching to get the IP.

ip a | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"

ip_grep But this gives all the IPs, we only require the second one in order to get that we use sed to filter the output.

ip a | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | sed -n '2p'

ip_sed

Final oneliner

We now combine this with echo and pipe the output into qr and then start the server to get things up and running. I personally add this whole with an alias in my .bashrc for ease of use; alternatively a script can also be created.

echo "http://$(ip a | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | sed -n '2p'):1234/" | qr && python -m http.server 1234

final

I use python-qrcode to generate qrcodes.