• Very simple graphical HTTP file server on your local network


    Hi!
    There are lots of one liners to create a simple http based file server on your local network.

    Here’s another, but with a yad twist (simple graphical user interface)!
    And it can open and close a certain port of your server’s firewall, so that your firewall does not prevent your client from downloading files from the server.

    This simple server can be used, for example, to share files from an Antergos PC (the server) to your phone, tablet, or another machine on the local network. Files are shown from the current working directory, or with an option you can set the directory (root) that the server shows.

    The client can simply use a web browser and download files from your local server. By default, the port 8000 is used by the server. So, for example in Firefox, you can give address like

    http://192.168.1.155:8000/
    

    and see the files on the server with address 192.168.1.155 on your local network.

    And when you are done sharing the files, simply stop/quit the server, and all is as normal again.

    If you want to improve it, please put your changes into this thread!

    EDIT 2018-01-29: modified the text above to reflect the current status of the script (currently post number 12 in this thread contains the latest version).

    #!/bin/bash
    #
    # A simple HTTP file server (ASHFS) with a (Yad based) GUI, for local network.
    #
    # Requirements:
    #    - python 3
    #    - yad
    #    - firewall does not prevent the port number this server uses (see $SRV_PORT below)
    #
    # You can verify (on the server) that this server is running (or not)
    # e.g. with this command:
    #    ps -ef | grep http.server | grep -v yad | grep -v grep
    #
    # A client machine may use the server e.g. with a browser:
    #    firefox http://servername-or-ip:portnr
    # For example:
    #    firefox http://192.168.1.101:8000
    #
    # Note 1: The http server initially stops all of its existing server instances.
    # Note 2: If 'ufw' firewall is enabled, then this http server disables ufw,
    #         and after finishing the http server, ufw is enabled.
    #
    # Written by: @manuel at Antergos.com
    # Date:       2018-01-27
    # Changes: 
    
    Stop()
    {
        local yadfield="$1"
        SRV_STATUS=stopped
        echo "$yadfield:$SRV_STATUS"       # output to yad field (Server status)
        pkill -fx "$SRV_COMMAND"
    }
    Start()
    {
        local yadfield="$1"
        SRV_STATUS=started
        echo "$yadfield:$SRV_STATUS"       # output to yad field (Server status)
        $SRV_COMMAND >/dev/null &
    }
    Restart()
    {
        local yadfield="$1"
        Stop  "$yadfield"
        Start "$yadfield"
    }
    
    export -f Stop Start Restart
    
    export SRV_PORT=8000                # port may be changed
    export SRV_COMMAND="python -m http.server $SRV_PORT"
    export SRV_STATUS="[unknown]"
    
    Constructor()
    {
        # If Firewall (ufw) is on, disable it while http server is running,
        # and enable firewall again when the http server stops.
    
        if [ "$(which ufw 2>/dev/null)" != "" ] ; then
            if [[ "$(gksu -w "ufw status")" =~ "active" ]] ; then
                gksu -w "ufw disable"                    # disable firewall now
                trap "{ gksu -w 'ufw enable' ; }" EXIT   # enable firewall at exit
            fi
        fi
    }
    
    StartHere()
    {
        Constructor
    
        local address=$(uname -n):$SRV_PORT
        # If 'uname -n' does not work in your system, use e.g. this:
        # address=$(ip addr | grep "inet " | grep -v "127.0" | head -n 1 | awk '{print $2}' | sed 's|/24||'):$SRV_PORT
    
        Restart    # There may be old instances, stop them first.
    
        yad --form \
            --title="Simple HTTP Server at $address" \
            --width=400 \
            --field="Server status":RO    "$SRV_STATUS" \
            --field="Start":fbtn          '@bash -c "Restart 1"' \
            --field="Stop":fbtn           '@bash -c "Stop 1"' \
            --button=gtk-quit:1
    
        Stop
    }
    
    StartHere "[email protected]"
    
  • @manuel said in Very simple graphical HTTP file server on your local network:

    Note 2: If ‘ufw’ firewall is enabled, then this http server disables ufw,

    “Note 2: If ‘ufw’ firewall is enabled, then this http server disables ufw,”

    Instead of disabling the firewall, would be nice to just add a rule and then delete a rule, to allow the program to run while firewall is still on.

    On start: ufw allow 8000/tcp
    On end: ufw delete allow 8000/tcp

  • Add requisites for “gksu” also, because I did not had it installed

  • Stop()
    {
    local yadfield="$1"
    SRV_STATUS=stopped
    echo “$yadfield:$SRV_STATUS” # output to yad field (Server status)
    gksu -w “ufw delete allow 8000/tcp"
    pkill -fx “$SRV_COMMAND”
    }
    Start()
    {
    local yadfield=”$1"
    SRV_STATUS=started
    echo “$yadfield:$SRV_STATUS” # output to yad field (Server status)
    gksu -w “ufw allow 8000/tcp”
    $SRV_COMMAND >/dev/null &
    }

  • Forgot to say that I’ve already used it, since I’m fixing a computers friend and it’s so easy to get documents from the other computer.
    It’s a small big utility. Thank you manu.

  • @casquinhas
    Many thanks for these improvements! 🙂

    I already anticipated that someone is going to improve the firewall part. I just did a quick hack that suited my usage, but managing only one port is much better than unnecessarily managing all of them.

    And yes, it uses gksu (needs to be a requirement as you mentioned) since I didn’t find a better solution. 😉

  • Thank you so much @manuel :). I’m about to try it out myself!

    Keep trying, never give up. In the end, you will find that it was all worth it

    My ISO: https://rebornos.wordpress.com

  • @keegan
    Thanks! And happy trial. If there are any comments or questions, please don’t hesitate to ask! 🙂

    And @casquinhas thanks for testing and your very useful comments!

  • It works perfectly! I just have one little question. I know I should know this, but seeing as I don’t seem to, I might as well ask - even though it’s just a silly question. How do you determine what you are hosting on the server created by this? Is it whatever directory you happen to be in when you execute your script?

    Thanks!

    Keep trying, never give up. In the end, you will find that it was all worth it

    My ISO: https://rebornos.wordpress.com

  • @keegan
    It is by no means a silly question! 🙂

    Basically it uses the current working directory, i.e. shows the file structure starting from the directory where e.g. the terminal currently is when starting the program.

    But if it has no given working directory (e.g. in a .desktop file), then it seems to start at the $HOME directory. That must be a feature from the python environment that starts the server, since this script is not setting it.

    But, your question raised the obvious idea, that the script should

    • show the starting directory
    • maybe have an option to set the starting directory

    So thank you very much for asking this! 🙂 Now I must make some improvements, but still try to keep this script as small as possible.

  • Hi guys!

    Here’s a new version that includes the improvements discussed above.
    Enjoy! 🙂

    #!/bin/bash
    #
    # Simple HTTP server with a (Yad based) GUI, for local network.
    #
    # Usage: for options, see variable $USAGE below!
    #
    # Requirements:
    #    - python 3
    #    - yad
    #    - firewall does not prevent the port number this server uses (see $SRV_PORT below)
    #    - uses package gksu for certain administrative tasks
    #
    # You can verify (on the server) that this server is running (or not)
    # e.g. with this command:
    #    ps -ef | grep http.server | grep -v yad | grep -v grep
    #
    # A client machine may use the server e.g. with a browser:
    #    firefox http://servername-or-ip:portnr
    # For example:
    #    firefox http://192.168.1.101:8000
    #
    # Note 1: The http server initially stops all of its existing server instances.
    # Note 2: If 'ufw' firewall is enabled, then this http server opens ufw port $SRV_PORT,
    #         and after stopping the http server, the port is closed.
    #
    # Written by: @manuel at Antergos.com
    # Date:       2018-01-27
    # Changes:    2018-01-29:
    #                 - managing only port $SRV_PORT of the firewall (thanks casquinhas!)
    #                 - added info about starting directory
    #                 - added option --dir=<starting-directory> (thanks Keegan!)
    
    
    export SRV_PORT=8000                                  # port may be changed!
    export SRV_COMMAND="python -m http.server $SRV_PORT"  # this implements the server!
    export SRV_STATUS="[unknown]"
    
    USAGE="Usage: $0 [--dir=<starting-directory>]"
    
    
    _message()  # show a message with yad
    {
        # User may give extra options (e.g. --geometry=AxB+C+D)
        # before the actual message.
        local opts xx
        for xx in "[email protected]"
        do
            case "$xx" in
                --*) opts+="$xx " ; shift ;;
                *) break ;;
            esac
        done
    
        local line="$*"
        local width=$((${#line} * 8))
        local width_limit=600
        if [ $width -gt $width_limit ] ; then
            width=$width_limit
        fi
    
        yad --form --title="Message" \
            --width=$width           \
            --button=gtk-quit:1      \
            --field="":TXT "$line"          $opts
    }
    
    Stop()
    {
        local yadfield="$1"
        SRV_STATUS=stopped
        echo "$yadfield:$SRV_STATUS"       # output to yad field (Server status)
        if [ $HAS_UFW -eq 1 ] ; then
            gksu -w "ufw delete allow $SRV_PORT/tcp"
        fi
        pkill -fx "$SRV_COMMAND"
    }
    Start()
    {
        local yadfield="$1"
        SRV_STATUS=started
        echo "$yadfield:$SRV_STATUS"       # output to yad field (Server status)
        if [ $HAS_UFW -eq 1 ] ; then
            gksu -w "ufw allow $SRV_PORT/tcp"
        fi
        $SRV_COMMAND >/dev/null &
    }
    Restart()
    {
        local yadfield="$1"
        Stop  "$yadfield"
        Start "$yadfield"
    }
    
    export -f Stop Start Restart  # for yad!
    
    Constructor()
    {
        # If Firewall (ufw) is on, prepare for disabling port $SRV_PORT while http server is running,
        # and enabling it again when the http server stops.
    
        HAS_UFW=0
        if [ "$(which ufw 2>/dev/null)" != "" ] ; then              # is ufw installed
            if [[ "$(gksu -w "ufw status")" =~ "active" ]] ; then   # if ufw enabled
               HAS_UFW=1
            fi
        fi
    
        # Handle user options:
        local xx
        local tmp
    
        for xx in "[email protected]"
        do
            case "$xx" in
                --dir=*)               # use given starting directory
                    tmp="${xx:6}"
                    case "$tmp" in
                        ~*) tmp="$HOME"${tmp:1} ;;
                    esac
                    cd "$tmp"
                    ;;
                -*)
                    _message "Error: option '$*' not supported.\n$USAGE"
                    exit 1
                    ;;
            esac
        done
    }
    
    # trap "{ gksu -w 'ufw delete allow 8000/tcp' ; }" EXIT   # enable firewall at exit
    
    StartHere()
    {
        Constructor "[email protected]"
    
        local address=$(uname -n):$SRV_PORT
        # If 'uname -n' does not work in your system, use e.g. this:
        # address=$(ip addr | grep "inet " | grep -v "127.0" | head -n 1 | awk '{print $2}' | sed 's|/24||'):$SRV_PORT
    
        Restart    # There may be old instances, stop them first.
    
        yad --form \
            --title="Simple HTTP Server at $address" \
            --width=400 \
            --field="Server status":RO    "$SRV_STATUS" \
            --field="Directory root":RO   "$PWD" \
            --field="Start":fbtn          '@bash -c "Restart 1"' \
            --field="Stop":fbtn           '@bash -c "Stop 1"' \
            --button=gtk-quit:1
    
        Stop
    }
    
    StartHere "[email protected]"
    
  • Thank you so much @manuel for clarifying that for me. I assumed that was what was going on, but your confirmation makes me feel better and more in control of my system when I use it. It’s always great to be well-informed😄.

    Keep trying, never give up. In the end, you will find that it was all worth it

    My ISO: https://rebornos.wordpress.com

  • Going to try out your new script right now!

    Thanks again.

    Keep trying, never give up. In the end, you will find that it was all worth it

    My ISO: https://rebornos.wordpress.com

  • It works perfectly 🙂 . Thank you so much for putting all this together @manuel! Is it fine with you if I make this into a package and include it in Reborn OS?

    Thanks!

    Keep trying, never give up. In the end, you will find that it was all worth it

    My ISO: https://rebornos.wordpress.com

  • @keegan
    Thanks for your kind words! 😊

    And sure, feel free to make it a package for the Reborn OS. But as a disclaimer, I won’t be responsible in any way for any damage this script might cause. 🙂

  • @manuel said in Very simple graphical HTTP file server on your local network:

    @keegan
    Thanks for your kind words! 😊

    And sure, feel free to make it a package for the Reborn OS. But as a disclaimer, I won’t be responsible in any way for any damage this script might cause. 🙂

    @manuel, now you need to give this program support for at least 20 years. I expect 30. But accept 20.

  • Of course I won’t hold you responsible 🙂 . If it would make you feel better, I can just say that “a helpful Antergos user created this”, so as to avoid other users potentially coming to you with their problems in the future. What do you think? Is there some other wording you would prefer even more?

    And thank you so much for letting me use this @manuel. Thank you.

    Keep trying, never give up. In the end, you will find that it was all worth it

    My ISO: https://rebornos.wordpress.com

  • @keegan
    Just to clarify my point of view:

    As this script is just a GUI wrapper to a one liner that actually implements the server, I’m not prepared to develop new features to it, because that would trash the original idea of creating the script.

    And if there are any security or major usability issues with this script, I don’t have the required resources to analyze them properly so I most likely cannot provide fixes for them without appropriate community contributions.

    Having said that, I’d still like to (indirectly) hear about usage reports, if there are any, in order to see how people are using it, and if I can help with potential problems. But I’m simply not prepared to handle a flood of problem reports, anyway.

    So the wording you mentioned is OK for me. I believe you get the picture where I stand. But please don’t hesitate to ask if it is not clear. 🙂

  • @casquinhas

    now you need to give this program support for at least 20 years. I expect 30. But accept 20.

    Hahaha… I surely would like to live that long! 🙂
    But then, someone may just fork this … 😉

graphical9 simple6 http1 Posts 24Views 358
Log in to reply
Bloom Email Optin Plugin

Looks like your connection to Antergos Community Forum was lost, please wait while we try to reconnect.