Page 1 of 1

Quick way to share files via LAN (python webserver script)

Posted: Wed May 08, 2024 7:16 pm
by FloraMae

I'm not really sure where to post this, so forgive me if this is not the right place.

I was going over some scripts I have and decided to do a quick bash script to start a simple python webserver which can be used to share files over LAN via a browser or similar. Could probably be better but might be useful to some people.

To use, copy the code to a new file. Open terminal and chmod +x the new file, then ./newfile 8080 to start a server on port 8080 using the contents of the current directory.

Then go to other LAN computer, open a browser and put in the local IP of the computer running the script and the port number.
example, if computer running the script is 192.168.1.156 then you would go to http://192.168.1.156:8080

Code: Select all

#!/bin/bash
####################################################################################
##
## Tiny python 2/3 HTTP server
##
## The below will start a server on port 8080, adjust your firewall if needed
## cd /path/to/files/to/serve
## server 8080
##
## CTRL + C to stop
##
###################################################################################

if which python3 >/dev/null; then
    echo 'python3'
    python3 -m http.server "$1"
    exit 0
else
    if which python2 >/dev/null; then
        echo 'python2'
        python2 -m SimpleHTTPServer "$1"
        exit 0
    else
        echo 'no python'
        exit 1
    fi
fi