#!/usr/bin/env python # # Guestbook in Python # # I installed the minimal Raspberry PI """ Raspberry Pi OS Lite Release date: November 19th 2024 System: 32-bit Kernel version: 6.6 Debian version: 12 (bookworm) Size: 508MB """ # # Raspberry Pi OS Lite Release date: November 19th 2024 System: 32-bit Kernel version: 6.6 Debian version: 12 (bookworm) Size: 508MB # # It would be possible to install the LAMP stack, # Howevery this already has two web servers you can use. # * busybox httpd # * python -m httpd.server 8080 & # # To get it to boot on power up set up /etc/rc.local # # scp pi@patrickpi:/etc/rc.local rc.local.txt # """ #!/bin/sh -e # owned by root # chmod 755 /etc/rc.local #!/usr/bin/ env python # # You could start a webserver using:- # python -m http.server 8080 & # Print the IP address _IP=$( /usr/bin/hostname -I) || true if ["$_IP" ]; then # printf "My IP address is %s\n" "$_IP" printf "My IP address is %s\n" fi # log last boot time for debug purposes date > /home/pi/rclocal.txt /usr/bin/env >> /home/pi/rclocal.txt /home/pi/iic/a.out ====boot1==== & sleep 1 /home/pi/iic/a.out ====boot2==== & /usr/bin/perl /home/pi/server_iot.pl & cd /home/pi #python3 -m http.server & #/home/pi/test.sh & /usr/bin/busybox httpd -p 0.0.0.0:8086 -h /home/pi/www & exit 0 """ # # Guestbook script in Python - gb.py # # A just about working guestbook suitable for use on my home lan. # some escaping of ' and " is to be done. # multi line comments seem to be working # import os import sys import urllib #from urllib.parse import urlparse from urllib.parse import urlparse, parse_qs # Send an HTTP header indicating the content type as HTML print("Content-type: text/html\n\n") # Start an HTML document with center-aligned content # style='text-align:center;' print( ''' ''' ) # Display a green heading print("

Guestbook

") print("
")

# for method=GET
query_string = os.environ.get("QUERY_STRING","")
#request_result = parse_qs(query_string)
#
print("GET:using query_string:")
print( query_string )
print( "===================")
# for method=POST
CONTENT_LENGTH = os.environ.get("CONTENT_LENGTH",0)
import sys
line=""
for line in sys.stdin:
  print(f"Input: {line}")

print("POST:using std_in:")
print( line )
print( "===================")
# add query string and stdin then parse
request_result = parse_qs( query_string+line )


#
# to be done:- convert end of line for JavaScript.
#
# lines end with \r,\n, \r\n 
# javaScript does not like multi line strings"
def gbField( p ):
  sep = "'"  
  f = request_result.get( p , "" )
  if ( f ):
    #return sep + f[0].replace("\r", "
\\") + sep return sep + (f[0].replace("\r", "
\\").replace('\"','\\\"').replace("\'","\\\'")) + sep return "" sep = "," import datetime now = datetime.datetime.now() ipAddress = database_url = os.environ.get("REMOTE_ADDR", "") print( "gbF( " + gbField( "name" ) + sep + gbField( "email" ) + sep + "\'"+str( now ) +" \'" + sep + "\'"+str(ipAddress)+" \'" + sep + gbField( "userfield1" ) + sep + gbField( "userfield2" ) + sep + gbField( "userfield3" ) + sep + gbField( "userfield4" ) + sep + gbField( "comments" ) + " ); " ) #For Python use () for multiline statements. a = ( "gbF( " + gbField( "name" ) + sep + gbField( "email" ) + sep + #"\'2025-01-24 10:49:01\'" + sep + "\'"+str( now )+" \'" + sep + #"\'127.0.0.1\'" + sep + "\'"+str(ipAddress)+" \'" + sep + gbField( "userfield1" ) + sep + gbField( "userfield2" ) + sep + gbField( "userfield3" ) + sep + gbField( "userfield4" ) + sep + gbField( "comments" ) + " ); " ) print( a ) # Open the file in write mode with open('/home/pi/www/gb/gbF.js', 'a') as file: # Write a string to the file file.write( a + ";\n") file.close() # # try out gbF in javaScript # print("try javascript" ) print("") print( "
ENV" ) # Retrieving the value of the "PATH" environment variable env_dict = os.environ print(env_dict) for key, value in env_dict.items(): print(f"{key}: {value}") print( "
" ) """ # Retrieving the value of the "PATH" environment variable path = os.environ["PATH"] print(path) #CONTENT_LENGTH: #CONTENT_LENGTH = os.environ["CONTENT_LENGTH"] # Retrieving the value of an environment variable with a default value database_url = os.environ.get("DATABASE_URL", "localhost:5432") print(database_url) """ # # try out gbF in javaScript # print("") print("
") print( '''

home

home |

Forms

form | form2

data: gbookFXXXxx data | data: gbF


Data:-

http://patrickpi:8086/gb/gbF.js
''' ) # Close the HTML document print("")