Sunday, December 11, 2022

Share Over Wifi in Linux

I sometimes need to transfer a photo from my Android phone to my Linux computer. Bluetooth works, but is slow. I found Send via Wifi in Total Commander on my Android phone, which transfers files very quickly. It provides an http address which I put into Firefox, where the files I have chosen to send as download links.

It is also possible to send files from Linux to Total Commander. The Receive via Wifi plugin says it requires either Total Commander (which is a Windows PC app only), or an app with a share button and a send via wifi feature, which I don't have.

As Total Commander seems to set up an http server to transfer files, I wondered if it was possible to start an http server in Linux so that Android can view and download files on the computer. Indeed it is. There is a simple Python command that will set up an http sever. This can then be accessed from a web browser in Android, and files viewed, played or downloaded, just by entering the IP address.

This of course is over a local network behind a firewall. 

As entering an IP address, is a bit tedious, I then wondered if there was a way of getting a QR code to pop up with the IP address for Firefox to read in Android. Well, yes there is.

Scan the QR code in Android, and Firefox asks if you want to open the link. (If you don't have Firefox for Android, you may need another QR reading app.) From the page that opens, you can browse the file system.

To do this you'll need the qrencode and imagemagick packages. The script I wrote to start the server and pop up the QR code is below. The exact Python command to start the http server may vary according to distro and version - this one works in Debian Bookworm. The port used must be open on the hosting computer.

#!/bin/bash
ip=$(hostname -I)
echo http://${ip/ /}:8000/ > url.txt
qrencode -s 8 -r url.txt -o qrcode.png
display qrcode.png &
python3 -m http.server

Mostly for my benefit, as I hacked together the script from bits and pieces I found on the internet, here's some information about what the code does. (I always forget after a while and wonder why I did what I did.)

1. Find the IP address of the host computer.

2. Trim the output because the command adds a space to the end of the variable for some reason and write the output to a text file because qrencode only accepts text strings.

3. Read the text file and turn it into a QR code with block width 8 pixels.

4. Display the QR code and start the server in parallel (&).

To stop the sever, do Ctrl + C.



No comments:

Post a Comment