#!/usr/bin/env python3
import os
import socket
import io
import time
from flask import Flask
app = Flask( __name__ )
# ---------------------------------------------------------------------
@app.route( "/" )
def home():
# get our IP address
try:
with socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) as sock:
sock.connect( ( "10.255.255.255", 1 ) )
ip_address = sock.getsockname()[ 0 ]
except Exception as ex:
ip_address = str( ex )
# get the message of the day
fname = os.environ.get( "MOTD" )
if not fname:
motd = "Embrace the void, there is no message of the day."
elif not os.path.isfile( fname ):
motd = "Can't find the MOTD file: {}".format( fname )
else:
with open( fname, "r", encoding="utf-8" ) as fp:
motd = fp.read()
# generate the response
buf = io.StringIO()
print( "Current time: {}".format( time.strftime( "%c" ) ), file=buf )
print( "
", file=buf )
print( "IP address: {}".format( ip_address ), file=buf )
print( "
Message of the day:", file=buf ) print( "
{} ".format( motd ), file=buf )
return buf.getvalue()
# ---------------------------------------------------------------------
# run the Flask webapp
app.run( host="0.0.0.0" )