Python web page to display SSL certificate status My task was to create a Python script and connect to various TLS sites and grab the expiry dates. I've done this with openssl and ssl modules just fine.

In my mind I was wanting to create a web page as a dashboard.

I've done that.

It's as simple as it gets I think (which I am happy with):



But I ended up doing a for loop of the dictionary I stored the values for expiration, url, and status.

I.E

with open(htmlfile,'a') as fh:

        for urlandport,data in urldict.items(): 
            url,tcpport = urlandport
            msg,status = data
            #print(url)        
            if status == 'OK':
                delta      = data[0] - now
                days_left  = str(abs(delta.days))
                color      = 'green'  if int(days_left) &gt; 400 \
                        else 'orange' if int(days_left) &lt; 100 \
                        else 'red'
                sclr       = 'green'
            else:
                days_left  = 'NA'
                expiration = 'NA' 
                color      = 'blue'
                sclr       = 'blue'
                msg,status = status,msg
                #Swap dashboard order on failure  
            fh.write(
                     "&lt;tr&gt;" + "&lt;td&gt;" + url + " " + str(tcpport) + "&lt;/td&gt;"     + \
                     "&lt;td&gt;&lt;font color="+color+"&gt;"+days_left+"&lt;/font&gt;"+"&lt;/td&gt;" + \
                     "&lt;td&gt;&lt;font color="+color+"&gt;"+ str(msg)+"&lt;/font&gt;"+"&lt;/td&gt;" + \
                     "&lt;td&gt;&lt;font color="+sclr+"&gt;"+str(status)+"&lt;/font&gt;"+"&lt;/td&gt;"+"&lt;/tr&gt;"
                     )


In my mind this does not seem very modern or flexible to proceed in manually cobbling HTML like this. It works. But I am wondering if I should invest in learning something more suitable for scalability.

I was also also to separate out the certs into 4 different tables: soon to expire, OK for now, connect failure and too old to bother and display then in a particular order for the user.

Hence, I ended up just creating 4 lists and appending to them so I could loop over each as I chose.

It just feels like I wrote some eye searing python to cobble something together...I am not even sure if a "Web Framework" would help or frankly why...I've looked into some of them but it's not sinking in.

Any suggestions?
It just feels like I wrote some eye searing python to cobble something together...I am not even sure if a "Web Framework" would help or frankly why...I've looked into some of them but it's not sinking in.


I think it would definitely be better then what you have currently

I can recommend flask, a small web microframework. 

Pretty easy to use and has plenty of features, it will handle serving the page, making nicely formatted html pages, handling routes.

If you want some pretty tables you could even use Flask-Table

But something simple as this should even work,

app.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def ssl_status():
    ssl_connections = [
        ["url.url", 165, "some-date-2019", "OK"]
    ]
    return render_template('ssl_status.html', ssl_connections=ssl_connections)

if __name__ == '__main__':
    app.run()


ssl_status.html

&lt;!doctype html&gt;
&lt;title&gt;SSL Status page&lt;/title&gt;
&lt;table&gt;
    {% for row in ssl_connections %}
    &lt;tr&gt;
        {% for item in row %}
        &lt;td&gt;{{ item }}&lt;/td&gt;
        {% endfor %}
    &lt;/tr&gt;
    {% endfor %}
&lt;/table&gt;


You'd need some .css probably to make the colors work, and some other stuff to make it look the same as your example.