├── .gitattributes ├── .gitignore ├── Readme.md ├── menubox.png ├── menubox.sh ├── settings.py ├── webpage.png ├── webserver-install.sh ├── webserver.py ├── webserver.sh ├── webserver3.py └── www ├── Readme.html ├── calculator.zip ├── images.zip └── webserver.txt /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # webserver.py 2 | ### Simple python Stand Alone Local Network Web Server 3 | #### Dynamically Reads folder and file entries into a right pane list. Selected folder opens in current browser tab 4 | with back arrow for navigation back. File links selected in right pane display contents in left pane. Refresh button 5 | re-reads directory structure for right pane link display. 6 | 7 | ## Quick Install 8 | Log into destination computer using Putty SSH or open a computer terminal session on the destination computer. 9 | This can be a Raspberry Pi, Debian or similar unix system that supports apt-get. 10 | ***Step 1*** Highlight curl command in code box below using mouse left button. Right click mouse in highlighted area and Copy. 11 | ***Step 2*** On RPI putty SSH or terminal session right click, select paste then Enter to download and run script. 12 | 13 | curl -L https://raw.github.com/pageauc/webserver/master/webserver-install.sh | bash 14 | 15 | The command will download and execute the GitHub ***webserver-install.sh*** bash script. 16 | Alternatively you can git clone the repository to the local system 17 | 18 | cd ~ 19 | git clone https://raw.github.com/pageauc/webserver 20 | 21 | Screen shot of web interface 22 |  23 | 24 | ## Description 25 | This is a single file stand alone python webserver that needs minimal or no setup. 26 | This webserver can be used to remotely view images, video, documents, html, java, Etc. 27 | files from your (LAN) local area network connected computers using a web browser. 28 | Directory links to files and subfolders are dynamically created and displayed 29 | in a right side listing that can be arranged and sorted via variables. 30 | Variables are contained in the settings.py file and allow customization of 31 | webserver display and configuration settings. 32 | 33 | I use webserver.py in my [pi-timolo](https://github.com/pageauc/pi-timolo), 34 | [speed-camera](https://github.com/pageauc/speed-camera) and other projects 35 | to allow easy browsing of image, video or other project files. 36 | 37 | If a folder is selected on the right pane listing 38 | the folder contents will appear in the existing browser tab. 39 | Select < BACK link to navigate back through folder structure. 40 | NOTE: < BACK link will not display in web root folder. 41 | 42 | This web server can display other types of content 43 | like javascript, css Etc will run. A sample calculator program is 44 | include in the calculator folder of the www web root. 45 | If a folder has an index.html it will take over 46 | the browser tab. html files will be displayed full screen in browser tab. 47 | 48 | This browser can only access local raspberry pi files. 49 | It cannot access internet web pages. 50 | 51 | ***Please Note*** 52 | You will need at least two entries in a folder 53 | before the web server will display content. 54 | 55 | ***Change Web Root*** 56 | Edit the settings.py to change the default www folder web root to 57 | another folder location of your choice. 58 | 59 | cd ~/webserver 60 | nano settings.py 61 | 62 | ## Manual Install 63 | From logged in RPI SSH session or console terminal perform the following. You can review 64 | the webserver-install.sh script code before executing. 65 | 66 | cd ~ 67 | wget https://raw.github.com/pageauc/webserver/master/webserver-install.sh 68 | chmod +x webserver-install.sh 69 | ./webserver-install.sh 70 | 71 | ## How to Run webserver.py 72 | 73 | Screen shot of menubox.sh whiptail Menu 74 |  75 | 76 | Use menubox.sh for an easy way to manage the webserver 77 | 78 | cd ~/webserver 79 | ./menubox.sh 80 | 81 | or to manually start webserver.py in the foreground (ctl-c to stop) 82 | Note if you exit the terminal session the webserver will exit. See below 83 | for manually running webserver.py in background using webserver.sh 84 | 85 | cd ~/webserver 86 | ./webserver.py 87 | 88 | A message will be displayed indicating the ip address:port to enter in 89 | a browser url bar. This will access the webserver files in the server web root. 90 | 91 | If you want to run the webserver.py as a background daemon then use webserver.sh 92 | If no parameter is specified it will display the PID or indicate webserver.py is 93 | not running. 94 | 95 | To Start webserver.py as a background daemon 96 | 97 | cd ~/webserver 98 | ./webserver.sh start 99 | 100 | To Stop webserver.py 101 | 102 | cd ~/webserver 103 | ./webserver.sh stop 104 | 105 | ## Run webserver.py on Boot 106 | 107 | To auto launch webserver.py on boot-up of raspberry pi perform the following 108 | 109 | sudo nano /etc/rc.local 110 | 111 | In nano add the following command to the rc.local file just before the exit 0 command. 112 | This will launch webserver and the webserver in the background running under the pi user (not root). 113 | Note the webserver startup is optional. 114 | 115 | su pi -c "/home/pi/webserver/webserver.sh start > /dev/null" 116 | 117 | ctrl-x y to save and exit nano editor 118 | 119 | Reboot RPI and test operation by triggering motion and checking images are successfully saved to your motion or timelapse folder. 120 | 121 | sudo reboot 122 | 123 | use menubox.sh to verify that webserver.py is running after reboot 124 | 125 | ## Customize Settings 126 | 127 | The webserver.py variables are stored in the settings.py file. These can be 128 | edited using the nano editor to customize for your particular needs if required 129 | 130 | Edit settings.py file using nano editor per the following commands 131 | 132 | cd ~/webserver 133 | nano settings.py 134 | 135 | ctrl-x y to exit nano editor and save changes 136 | 137 | You can also use menubox.sh to edit/view the webserver settings 138 | 139 | cd ~/webserver 140 | ./menubox.sh 141 | 142 | ## Reference Links 143 | webserver wiki - https://github.com/pageauc/pi-timolo/wiki/Access-images-via-webserver 144 | github webserver repo - https://github.com/pageauc/webserver 145 | 146 | 147 | Good Luck 148 | Claude Pageau 149 | -------------------------------------------------------------------------------- /menubox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pageauc/webserver/5ffc4731af4291546ef270e35e640b83d34ecda5/menubox.png -------------------------------------------------------------------------------- /menubox.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ver="1.20" 4 | 5 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 6 | cd $DIR 7 | 8 | pyconfigfile="./settings.py" 9 | filename_conf="./settings.conf" 10 | filename_temp="./settings.conf.temp" 11 | 12 | #------------------------------------------------------------------------------ 13 | function do_anykey () 14 | { 15 | echo "" 16 | echo "######################################" 17 | echo "# Review Output #" 18 | echo "######################################" 19 | read -p " Press Enter to Return to Main Menu" 20 | } 21 | 22 | #------------------------------------------------------------------------------ 23 | function init_status () 24 | { 25 | if [ -z "$( pgrep -f webserver.py )" ]; then 26 | WEB_1="START" 27 | WEB_2="webserver.py in background" 28 | else 29 | myip=$( ifconfig -a | grep 'inet ' | grep -v 127.0.0 | head -n 1 | tr -s " " | cut -d " " -f 3 ) 30 | myport=$( grep "web_server_port" settings.py | cut -d "=" -f 2 | cut -d "#" -f 1 | awk '{$1=$1};1' ) 31 | webserver_pid=$( pgrep -f webserver.py ) 32 | WEB_1="STOP" 33 | WEB_2="webserver.py - PID is $webserver_pid http://$myip:$myport" 34 | fi 35 | } 36 | 37 | #------------------------------------------------------------------------------ 38 | function do_webserver () 39 | { 40 | if [ -z "$( pgrep -f $DIR/webserver.py )" ]; then 41 | ./webserver.sh start 42 | if [ -z "$( pgrep -f $DIR/webserver.py )" ]; then 43 | whiptail --msgbox "Failed to Start webserver.py Please Investigate Problem." 20 70 44 | else 45 | myip=$( ifconfig -a | grep 'inet ' | grep -v 127.0.0 | head -n 1 | tr -s " " | cut -d " " -f 3 ) 46 | myport=$( grep "web_server_port" settings.py | cut -d "=" -f 2 | cut -d "#" -f 1 | awk '{$1=$1};1' ) 47 | whiptail --msgbox --title "Webserver Access" "Access web server from another network computer web browser using url http://$myip:$myport" 15 60 48 | fi 49 | else 50 | $DIR/webserver.sh stop 51 | if [ ! -z "$( pgrep -f $DIR/webserver.py )" ]; then 52 | whiptail --msgbox "Failed to Stop webserver.py Please Investigate Problem." 20 70 53 | fi 54 | fi 55 | do_main_menu 56 | } 57 | 58 | #------------------------------------------------------------------------------ 59 | function do_webserver_config () 60 | { 61 | if [ -e $DIR/settings.py ] ; then 62 | /bin/nano $DIR/settings.py 63 | else 64 | whiptail --msgbox "ERROR - $DIR/settings.py File Not Found. Please Investigate." 20 65 1 65 | fi 66 | } 67 | 68 | #-------------------------------------------------------------------- 69 | function do_edit_save () 70 | { 71 | if (whiptail --title "Save $var=$newvalue" --yesno "$comment\n $var=$newvalue was $value" 8 65 --yes-button "Save" --no-button "Cancel" ) then 72 | value=$newvalue 73 | 74 | rm $filename_conf # Initialize new conf file 75 | while read configfile ; do 76 | if echo "${configfile}" | grep --quiet "${var}" ; then 77 | echo "$var=$value #$comment" >> $filename_conf 78 | else 79 | echo "$configfile" >> $filename_conf 80 | fi 81 | done < $pyconfigfile 82 | cp $filename_conf $pyconfigfile 83 | fi 84 | do_settings_menu 85 | } 86 | 87 | #-------------------------------------------------------------------- 88 | function do_edit_variable () 89 | { 90 | choice=$(cat $filename_temp | grep $SELECTION) 91 | 92 | var=$(echo $choice | cut -d= -f1) 93 | value=$(echo $choice | cut -d= -f2) 94 | comment=$( cat $filename_conf | grep $var | cut -d# -f2 ) 95 | 96 | echo "${value}" | grep --quiet "True" 97 | # Exit status 0 means anotherstring was found 98 | # Exit status 1 means anotherstring was not found 99 | if [ $? = 0 ] ; then 100 | newvalue=" False" 101 | do_edit_save 102 | else 103 | echo "${value}" | grep --quiet "False" 104 | if [ $? = 0 ] ; then 105 | newvalue=" True" 106 | do_edit_save 107 | elif [ $? = 1 ] ; then 108 | newvalue=$(whiptail --title "Edit $var (Enter Saves or Tab)" \ 109 | --inputbox "$comment\n $var=$value" 10 65 "$value" \ 110 | --ok-button "Save" 3>&1 1>&2 2>&3) 111 | exitstatus=$? 112 | if [ ! "$newvalue" = "" ] ; then # Variable was changed 113 | if [ $exitstatus -eq 1 ] ; then # Check if Save selected otherwise it was cancelled 114 | do_edit_save 115 | elif [ $exitstatus -eq 0 ] ; then 116 | echo "do_edit_variable - Cancel was pressed" 117 | if echo "${value}" | grep --quiet "${newvalue}" ; then 118 | do_settings_menu 119 | else 120 | do_edit_save 121 | fi 122 | fi 123 | fi 124 | fi 125 | fi 126 | do_settings_menu 127 | } 128 | 129 | #-------------------------------------------------------------------- 130 | function do_edit_menu () 131 | { 132 | clear 133 | echo "Copy $filename_conf from $pyconfigfile Please Wait ...." 134 | cp $pyconfigfile $filename_conf 135 | echo "Initialize $filename_temp Please Wait ...." 136 | cat $filename_conf | grep = | cut -f1 -d# | tr -s [:space:] >$filename_temp 137 | echo "Initializing Settings Menu Please Wait ...." 138 | menu_options=() 139 | while read -r number text; do 140 | menu_options+=( ${number//\"} "${text//\"}" ) 141 | done < $filename_temp 142 | 143 | SELECTION=$( whiptail --title "webserver Settings Menu" \ 144 | --menu "Arrow/Enter Selects or Tab" 0 0 0 "${menu_options[@]}" --ok-button "Edit" 3>&1 1>&2 2>&3 ) 145 | RET=$? 146 | if [ $RET -eq 1 ] ; then 147 | do_settings_menu 148 | elif [ $RET -eq 0 ]; then 149 | cp $pyconfigfile $filename_conf 150 | cat $filename_conf | grep = | cut -f1 -d# | tr -s [:space:] >$filename_temp 151 | do_edit_variable 152 | fi 153 | } 154 | 155 | #------------------------------------------------------------------------------ 156 | function do_nano_main () 157 | { 158 | cp $pyconfigfile $filename_conf 159 | nano $filename_conf 160 | if (whiptail --title "Save Nano Edits" --yesno "Save nano changes to $filename_conf\n or cancel all changes" 8 65 --yes-button "Save" --no-button "Cancel" ) then 161 | cp $filename_conf $pyconfigfile 162 | fi 163 | } 164 | 165 | #------------------------------------------------------------------------------ 166 | function do_settings_menu () 167 | { 168 | SET_SEL=$( whiptail --title "Settings Menu" --menu "Arrow/Enter Selects or Tab Key" 0 0 0 --ok-button Select --cancel-button Back \ 169 | "a " "Menu Edit webserver settings.py" \ 170 | "b " "nano Edit settings.py" \ 171 | "c " "View settings.py" \ 172 | "d " "Back to Main Menu" 3>&1 1>&2 2>&3 ) 173 | 174 | RET=$? 175 | if [ $RET -eq 1 ]; then 176 | clear 177 | rm -f $filename_temp 178 | rm -f $filename_conf 179 | do_main_menu 180 | elif [ $RET -eq 0 ]; then 181 | case "$SET_SEL" in 182 | a\ *) do_edit_menu 183 | do_settings_menu ;; 184 | b\ *) do_nano_main 185 | do_settings_menu ;; 186 | c\ *) more -d settings.py 187 | do_anykey 188 | do_settings_menu ;; 189 | d\ *) clear 190 | rm -f $filename_temp 191 | rm -f $filename_conf 192 | do_main_menu ;; 193 | *) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;; 194 | esac || whiptail --msgbox "There was an error running selection $SELECTION" 20 60 1 195 | fi 196 | 197 | } 198 | 199 | #------------------------------------------------------------------------------ 200 | function do_upgrade () 201 | { 202 | if (whiptail --title "GitHub Upgrade webserver" --yesno "Upgrade webserver files from GitHub" 8 65 --yes-button "upgrade" --no-button "Cancel" ) then 203 | curl -L https://raw.github.com/pageauc/webserver/master/webserver-install.sh | bash 204 | do_anykey 205 | fi 206 | } 207 | 208 | #------------------------------------------------------------------------------ 209 | function do_about () 210 | { 211 | whiptail --title "About" --msgbox " \ 212 | webserver.py 213 | Manage webserver operation and settings 214 | 215 | written by Claude Pageau 216 | 217 | for more information 218 | 219 | View Readme.md (use menubox.sh help option) 220 | or 221 | visit web repo at https://github.com/pageauc/webserver 222 | Raise a github issue for issues or questions 223 | 224 | \ 225 | " 0 0 0 226 | } 227 | 228 | #------------------------------------------------------------------------------ 229 | function do_main_menu () 230 | { 231 | init_status 232 | 233 | SELECTION=$(whiptail --title "Webserver Main Menu" --menu "Arrow/Enter Selects or Tab Key" 0 0 0 --cancel-button Quit --ok-button Select \ 234 | "a $WEB_1" "$WEB_2" \ 235 | "b SETTINGS" "Edit/View webserver Settings" \ 236 | "c HELP" "View Readme.md" \ 237 | "d UPGRADE" "Files From GitHub.com" \ 238 | "e ABOUT" "This Program" \ 239 | "q QUIT" "Exit menubox.sh" 3>&1 1>&2 2>&3) 240 | 241 | RET=$? 242 | if [ $RET -eq 1 ]; then 243 | exit 0 244 | elif [ $RET -eq 0 ]; then 245 | case "$SELECTION" in 246 | a\ *) do_webserver ;; 247 | b\ *) do_settings_menu ;; 248 | c\ *) more -d Readme.md 249 | do_anykey ;; 250 | d\ *) do_upgrade ;; 251 | e\ *) do_about ;; 252 | q\ *) clear 253 | exit 0 ;; 254 | *) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;; 255 | esac || whiptail --msgbox "There was an error running selection $SELECTION" 20 60 1 256 | fi 257 | } 258 | 259 | #------------------------------------------------------------------------------ 260 | # Main Script 261 | #------------------------------------------------------------------------------ 262 | if [ $# -eq 0 ] ; then 263 | while true; do 264 | do_main_menu 265 | done 266 | fi -------------------------------------------------------------------------------- /settings.py: -------------------------------------------------------------------------------- 1 | # User Configuration variable settings for webserver 2 | # Done by - Claude Pageau 3 | 4 | configTitle = "webserver Default Settings" 5 | configName = "settings.py" 6 | 7 | #====================================== 8 | # webserver.py Settings 9 | #====================================== 10 | 11 | # Web Server settings 12 | # ------------------- 13 | web_server_port = 8090 # default= 8080 Web server access port eg http://192.168.1.100:8080 14 | web_server_root = "www" # default= "media" webserver root path to webserver image/video sub-folders 15 | web_page_title = "My Webserver" # web page title that browser show (not displayed on web page) 16 | web_page_refresh_on = True # Refresh True=On (per seconds below) False=Off (never) 17 | web_page_refresh_sec = "900" # default= "900" seconds to wait for web page refresh seconds (15 minutes) 18 | web_page_blank = False # True Starts left image with a blank page until a right menu item is selected 19 | # False displays second list[1] item since first may be in progressx 20 | 21 | # Left iFrame Image Settings 22 | # -------------------------- 23 | web_image_height = "768" # default= "768" px height of images to display in iframe 24 | web_iframe_width_usage = "75%" # Left Pane - Sets % of total screen width allowed for iframe. Rest for right list 25 | web_iframe_width = "100%" # Desired frame width to display images. can be eg percent "80%" or px "1280" 26 | web_iframe_height = "100%" # Desired frame height to display images. Scroll bars if image larger (percent or px) 27 | 28 | # Right Side Files List 29 | # --------------------- 30 | web_max_list_entries = 0 # 0 = All or Specify Max right side file entries to show (must be > 1) 31 | web_list_height = web_image_height # Right List - side menu height in px (link selection) 32 | web_list_by_datetime = True # True=datetime False=filename 33 | web_list_sort_descending = True # reverse sort order (filename or datetime per web_list_by_datetime setting 34 | 35 | 36 | # ---------------------------------------------- End of User Variables ----------------------------------------------------- 37 | -------------------------------------------------------------------------------- /webpage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pageauc/webserver/5ffc4731af4291546ef270e35e640b83d34ecda5/webpage.png -------------------------------------------------------------------------------- /webserver-install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Convenient webserver-install.sh script written by Claude Pageau 17-May-2017 3 | 4 | ver="1.0" 5 | APP_DIR='webserver' # Default folder install location 6 | 7 | cd ~ 8 | if [ -d "$APP_DIR" ] ; then 9 | STATUS="Upgrade" 10 | echo "Upgrade $APP_DIR Files" 11 | else 12 | echo "New $APP_DIR Install" 13 | STATUS="New Install" 14 | mkdir -p $APP_DIR 15 | mkdir -p $APP_DIR/www 16 | echo "$APP_DIR Folder Created" 17 | fi 18 | 19 | cd $APP_DIR 20 | INSTALL_PATH=$( pwd ) 21 | 22 | # Remember where this script was launched from 23 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 24 | 25 | echo "-------------------------------------------------------------" 26 | echo " webserver-install.sh script ver $ver" 27 | echo "-------------------------------------------------------------" 28 | echo " Downloading Files" 29 | wget -O settings.py -q --show-progress https://raw.github.com/pageauc/webserver/master/settings.py 30 | if [ $? -ne 0 ] ; then 31 | wget -O settings.py https://raw.github.com/pageauc/webserver/master/settings.py 32 | wget -O menubox.sh https://raw.github.com/pageauc/webserver/master/menubox.sh 33 | wget -O webserver.py https://raw.github.com/pageauc/webserver/master/webserver.py 34 | wget -O webserver.sh https://raw.github.com/pageauc/webserver/master/webserver.sh 35 | wget -O webserver-install.sh https://raw.github.com/pageauc/webserver/master/webserver-install.sh 36 | wget -O Readme.md https://raw.github.com/pageauc/webserver/master/Readme.md 37 | wget -O www/Readme.html https://raw.github.com/pageauc/webserver/master/www/Readme.html 38 | wget -O www/webserver.txt https://raw.github.com/pageauc/webserver/master/www/webserver.txt 39 | wget -O www/calculator.zip https://raw.github.com/pageauc/webserver/master/www/calculator.zip 40 | wget -O www/images.zip https://raw.github.com/pageauc/webserver/master/www/images.zip 41 | else 42 | wget -O menubox.sh -q --show-progress https://raw.github.com/pageauc/webserver/master/menubox.sh 43 | wget -O webserver.py -q --show-progress https://raw.github.com/pageauc/webserver/master/webserver.py 44 | wget -O webserver.sh -q --show-progress https://raw.github.com/pageauc/webserver/master/webserver.sh 45 | wget -O webserver-install.sh -q --show-progress https://raw.github.com/pageauc/webserver/master/webserver-install.sh 46 | wget -O Readme.md -q --show-progress https://raw.github.com/pageauc/webserver/master/Readme.md 47 | wget -O www/Readme.html -q --show-progress https://raw.github.com/pageauc/webserver/master/www/Readme.html 48 | wget -O www/webserver.txt -q --show-progress https://raw.github.com/pageauc/webserver/master/www/webserver.txt 49 | wget -O www/calculator.zip -q --show-progress https://raw.github.com/pageauc/webserver/master/www/calculator.zip 50 | wget -O www/images.zip -q --show-progress https://raw.github.com/pageauc/webserver/master/www/images.zip 51 | fi 52 | echo " Done Downloads" 53 | echo "-------------------------------------------------------------" 54 | echo " Make Required Files Executable" 55 | echo "" 56 | chmod +x *py 57 | chmod -x settings*py 58 | chmod +x *sh 59 | echo " Done Permissions" 60 | echo "-------------------------------------------------------------" 61 | echo "" 62 | 63 | cd www 64 | unzip -o calculator.zip 65 | unzip -o images.zip 66 | rm calculator.zip 67 | rm images.zip 68 | 69 | cd $DIR 70 | # Check if webserver-install.sh was launched from webserver folder 71 | if [ "$DIR" != "$INSTALL_PATH" ]; then 72 | if [ -e 'webserver-install.sh' ]; then 73 | echo "$STATUS Cleanup webserver-install.sh" 74 | rm webserver-install.sh 75 | fi 76 | fi 77 | 78 | echo "-----------------------------------------------" 79 | echo " $STATUS Complete to $INSTALL_PATH" 80 | echo "" 81 | echo "Instructions to Run this Webserver" 82 | echo "" 83 | echo " cd ~/webserver" 84 | echo " ./menubox.sh" 85 | echo "" 86 | echo "This will display a whiptail menu of options" 87 | echo "-----------------------------------------------" 88 | 89 | echo "Good Luck Claude ..." 90 | echo "Bye" 91 | 92 | -------------------------------------------------------------------------------- /webserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import cgi 4 | import os 5 | import socket 6 | import fcntl 7 | import struct 8 | import SocketServer 9 | import sys 10 | import time 11 | import urllib 12 | from SimpleHTTPServer import SimpleHTTPRequestHandler 13 | from StringIO import StringIO 14 | 15 | PROG_VER = "ver 3.7 written by Claude Pageau" 16 | ''' 17 | SimpleHTTPServer python program to allow selection of images from right panel and display in an iframe left panel 18 | Use for local network use only since this is not guaranteed to be a secure web server. 19 | based on original code by zeekay and modified by Claude Pageau Nov-2015 for use with pi-timolo.py on a Raspberry Pi 20 | from http://stackoverflow.com/questions/8044873/python-how-to-override-simplehttpserver-to-show-timestamp-in-directory-listing 21 | 22 | 1 - Use nano editor to change webserver.py web_server_root and other variables to suit at bottom of config.py 23 | nano config.py # Webserver settings are near the end of the file 24 | ctrl-x y to save changes 25 | 26 | 2 - On Terminal session execute command below. This will display file access information 27 | ./webserver.py # ctrl-c to stop web server. Note if you close terminal session webserver.py will stop. 28 | 29 | 3 - To Run this script as a background daemon execute the command below. 30 | Once running you can close the terminal session and webserver will continue to run. 31 | ./webserver.sh start 32 | To check status of webserver type command below with no parameter 33 | ./webserver.sh 34 | 35 | 4 - On a LAN computer web browser url bar, input this RPI ip address and port number per below 36 | example http://192.168.1.110:8080 37 | 38 | Variable Settings are imported from config.py 39 | ''' 40 | 41 | SCRIPT_PATH = os.path.abspath(__file__) # Find the full path of this python script 42 | BASE_DIR = os.path.dirname(SCRIPT_PATH) # Get the path location only (excluding script name) 43 | PROG_NAME = os.path.basename(__file__) # Name of this program 44 | net_interface_names = [ b'eth0', b'wlan0' ] # byte string list of interface names to check 45 | 46 | # Check for variable file to import and error out if not found. 47 | CONFIG_FILE_PATH = os.path.join(BASE_DIR, "settings.py") 48 | # Check if config file found and import variable settings. 49 | if not os.path.exists(CONFIG_FILE_PATH): 50 | print("ERROR - Cannot Import Configuration Variables.") 51 | print(" Missing Configuration File %s" % CONFIG_FILE_PATH) 52 | sys.exit(1) 53 | else: 54 | # Read Configuration variables from config.py file 55 | print("Importing Configuration Variables from File %s" % CONFIG_FILE_PATH) 56 | from settings import * 57 | 58 | os.chdir(web_server_root) 59 | web_root = os.getcwd() 60 | os.chdir(BASE_DIR) 61 | 62 | def get_ip_address(ifname): 63 | ''' 64 | Check network interface name to see if an ip address is bound to it 65 | return None of there is an IO error. Works with python2 and python3 66 | ''' 67 | try: 68 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 69 | return socket.inet_ntoa(fcntl.ioctl( 70 | s.fileno(), 71 | 0x8915, # SIOCGIFADDR 72 | struct.pack('256s', ifname[:15]) 73 | )[20:24]) 74 | except IOError: 75 | return None 76 | 77 | ip_list = [] 78 | for my_if in net_interface_names: 79 | my_ip = get_ip_address(my_if) 80 | if my_ip is not None: 81 | ip_list.append(my_ip) 82 | 83 | if web_list_by_datetime: 84 | dir_sort = 'Sort DateTime' 85 | else: 86 | dir_sort = 'Sort Filename' 87 | 88 | if web_list_sort_descending: 89 | dir_order = 'Desc' 90 | else: 91 | dir_order = 'Asc' 92 | 93 | list_title = "%s %s" % (dir_sort, dir_order) 94 | 95 | class DirectoryHandler(SimpleHTTPRequestHandler): 96 | 97 | def list_directory(self, path): 98 | try: 99 | list = os.listdir(path) 100 | all_entries = len(list) 101 | except os.error: 102 | self.send_error(404, "No permission to list directory") 103 | return None 104 | 105 | if web_list_by_datetime: 106 | # Sort by most recent modified date/time first 107 | list.sort(key=lambda x: os.stat(os.path.join(path, x)).st_mtime, reverse=web_list_sort_descending) 108 | else: 109 | # Sort by File Name 110 | list.sort(key=lambda a: a.lower(), reverse=web_list_sort_descending) 111 | f = StringIO() 112 | displaypath = cgi.escape(urllib.unquote(self.path)) 113 | # find index of first file or hyperlink 114 | 115 | file_found = False 116 | cnt = 0 117 | for entry in list: # See if there is a file for initializing iframe 118 | fullname = os.path.join(path, entry) 119 | if os.path.islink(fullname) or os.path.isfile(fullname): 120 | file_found = True 121 | break 122 | cnt += 1 123 | 124 | # Start HTML formatting code 125 | f.write('') 126 | f.write('
') 127 | # Setup Meta Tags and better viewing on small screen devices 128 | f.write('') 129 | f.write('') 130 | if web_page_refresh_on: 131 | f.write('' % web_page_refresh_sec) 132 | f.write('') 133 | 134 | tpath, cur_folder = os.path.split(self.path) 135 | f.write("')
187 | f.write('
')
197 | drive_status = df(MNT_POINT)
198 | f.write(b'
This is a single file stand alone python webserver that needs minimal or no setup. 10 | This webserver can be used to remotely view images, video, documents, html, java, Etc. 11 | files from your (LAN) local area network connected computers using a web browse. 12 | Directory links to files and subfolders are dynamically created and displayed 13 | in a right side listing that can be arranged and sorted via variables. 14 | Variables are contained in the settings.py file and allow customization of 15 | webserver display and configuration settings.
16 |I use webserver.py in my pi-timolo and other projects 17 | to allow easy browsing of image, video or other files.
18 |This web server can display other types of content 19 | like javascript, css Etc will run. A sample calculator program is 20 | include in the calculator folder of the www web root. 21 | If a folder has an index.html it will take over 22 | the browser tab. html files will be displayed full screen in browser tab.
23 |If a folder is selected on the right pane listing 24 | the folder contents will appear in a new browser tab.
25 |IMPORTANT 26 | You cannot navigate back to the previous folder from 27 | this new tab so when you are done with the folder 28 | tab just close it. You can navigate through a 29 | folder structure but remember each folder will 30 | open a new tab.
31 |This browser can only access local raspberry pi files. 32 | It cannot access internet web pages.
33 |Please Note 34 | You will need at least two entries in a folder 35 | before the web server will display content.
36 |Change Web Root 37 | Edit the settings.py to change the default www folder web root to 38 | another folder location of your choice.
39 |For Easy webserver-install.sh onto raspbian RPI.
42 |curl -L https://raw.github.com/pageauc/webserver/master/webserver-install.sh | bash
43 |
44 | From a computer logged into the RPI via SSH(Putty) or desktop terminal session
45 |From logged in RPI SSH session or console terminal perform the following. You can review 53 | the webserver-install.sh script code before executing.
54 |cd ~
55 | wget https://raw.github.com/pageauc/webserver/master/webserver-install.sh
56 | chmod +x webserver-install.sh
57 | ./webserver-install.sh
58 |
59 | Use menubox.sh for an easy way to manage the webserver
62 |cd ~/webserver
63 | ./menubox.sh
64 |
65 | or to manually start webserver.py in the foreground (ctl-c to stop) 66 | Note if you exit the terminal session the webserver will exit. See below 67 | for manually running webserver.py in background using webserver.sh
68 |cd ~/webserver
69 | ./webserver.py
70 |
71 | A message will be displayed indicating the ip address:port to enter in 72 | a browser url bar. This will access the webserver files in the server web root.
73 |If you want to run the webserver.py as a background daemon then use webserver.sh 74 | If no parameter is specified it will display the PID or indicate webserver.py is 75 | not running.
76 |To Start webserver.py as a background daemon
77 |cd ~/webserver
78 | ./webserver.sh start
79 |
80 | To Stop webserver.py
81 |cd ~/webserver
82 | ./webserver.sh stop
83 |
84 | To auto launch webserver.py on boot-up of raspberry pi perform the following
87 |sudo nano /etc/rc.local
88 |
89 | In nano add the following command to the rc.local file just before the exit 0 command. 90 | This will launch webserver and the webserver in the background running under the pi user (not root). 91 | Note the webserver startup is optional.
92 |su pi -c "/home/pi/webserver/webserver.sh start > /dev/null"
93 |
94 | ctrl-x y to save and exit nano editor
95 |Reboot RPI and test operation by triggering motion and checking images are successfully saved to your motion or timelapse folder.
96 |sudo reboot
97 |
98 | use menubox.sh to verify that webserver.py is running after reboot
99 |The webserver.py variables are stored in the settings.py file. These can be 102 | edited using the nano editor to customize for your particular needs if required
103 |Edit settings.py file using nano editor per the following commands
104 |cd ~/webserver
105 | nano settings.py
106 |
107 | ctrl-x y to exit nano editor and save changes
108 |You can also use menubox.sh to edit/view the webserver settings
109 |cd ~/webserver
110 | ./menubox.sh
111 |
112 | webserver wiki - https://github.com/pageauc/pi-timolo/wiki/Access-images-via-webserver
115 | github webserver repo - https://github.com/pageauc/webserver
Good Luck 117 | Claude Pageau
118 | -------------------------------------------------------------------------------- /www/calculator.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pageauc/webserver/5ffc4731af4291546ef270e35e640b83d34ecda5/www/calculator.zip -------------------------------------------------------------------------------- /www/images.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pageauc/webserver/5ffc4731af4291546ef270e35e640b83d34ecda5/www/images.zip -------------------------------------------------------------------------------- /www/webserver.txt: -------------------------------------------------------------------------------- 1 | webserver.py 2 | written by Claude Pageau 3 | 4 | This is a stand alone web server. 5 | 6 | Please Note 7 | You will need at least two entries in a folder 8 | before the web server will display content. 9 | IMPORTANT: Use browser refresh to update files list 10 | then select a list hyperlink since 11 | config.py default web server settings are 12 | 13 | web_page_refresh_on = False 14 | and 15 | web_page_blank = True 16 | 17 | If a folder is selected on the right pane listing 18 | the folder contents will appear in existing browser tab. 19 | Select the < BACK link on Right panel listing to 20 | navigate back to previous folder. < BACK will not 21 | be displayed when in web server root folder. 22 | Use browser refresh to update web page and display most 23 | recent data. 24 | 25 | This web server can display other types of content 26 | but html files will be displayed full screen. 27 | Web pages with javascript, css Etc will run. 28 | If a folder has an index.html it will take over 29 | the browser tab. 30 | This browser can only access local raspberry pi 31 | files. It cannot access internet web pages. 32 | 33 | For more information see 34 | github repo https://github.com/pageauc/webserver 35 | --------------------------------------------------------------------------------