#!/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( '''
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/patrickpi_styles.css">
</head>
<body>
<script language = "JavaScript" src = "/patrickpi_menu.js" type = "text/javascript"> </script>
<script>
function gbF( name, email, postedOn, IPaddress, userfield1, userfield2, userfield3, userfield4, comments){
// document.write( "<B>"+comments+"</B>" )
document.write( "<pre>" + postedOn + "</pre>" + comments + "<br><hr>" )
}
gbF( "users name", "users email", "timestamp postedOn", "users IPaddress", "uf1", "uf2", "uf3", "uf4", "comments");
</script>
''' )
# Display a green heading
print("<h1 style='color: green;'>Guestbook</h1>")
print("<pre>")
# 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", "<br>\\") + sep
return sep + (f[0].replace("\r", "<br>\\").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("<script>")
print( a )
print("</script>")
print( "<details><summary>ENV</summary>" )
# 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( "</details>" )
"""
# 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("<script>")
print( a )
print("</script>")
print("</pre>")
print(
'''
<hr>
<h2>home</h2>
<P><A href = "http://patrickpi:8086/index.html"> home </A> |
<h2>Forms</h2>
<P><A href = "http://patrickpi:8086/form.htm"> form </A> |
<A href = "http://patrickpi:8086/form2.htm"> form2 </A></P>
<P><A href = "http://patrickpi:8086/gb/gbookFXXX.js"> data: gbookFXXXxx data </A> |
<A href = "http://patrickpi:8086/gb/gbF.js"> data: gbF </A></P>
<hr>
<h2>Data:-</h2>
<details>
<summary>http://patrickpi:8086/gb/gbF.js</summary>
<script language = "JavaScript" type = "text/javascript"
src = "http://patrickpi:8086/gb/gbF.js" >
</script>
</details>
''' )
# Close the HTML document
print("</body></html>")