2
\$\begingroup\$

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):

enter image description here

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) > 400 \
                        else 'orange' if int(days_left) < 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(
                     "<tr>" + "<td>" + url + " " + str(tcpport) + "</td>"     + \
                     "<td><font color="+color+">"+days_left+"</font>"+"</td>" + \
                     "<td><font color="+color+">"+ str(msg)+"</font>"+"</td>" + \
                     "<td><font color="+sclr+">"+str(status)+"</font>"+"</td>"+"</tr>"
                     )

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?

\$\endgroup\$
  • \$\begingroup\$ It's counterintuitive to me that red indicates certificates with more than three months remaining. I would expect red to indicate imminent expiration. \$\endgroup\$ – 200_success Feb 1 at 0:28
  • \$\begingroup\$ Good catch....that was just an example page. The data is meaning less. \$\endgroup\$ – james6125 Feb 1 at 0:30
  • \$\begingroup\$ What do you mean by "just an example page"? On Code Review, we expect you to post real code from your project to review, and the code should be working correctly as intended. \$\endgroup\$ – 200_success Feb 1 at 0:34
  • \$\begingroup\$ It's test data in the same spirit that I don't work for example.com. The code is mine. \$\endgroup\$ – james6125 Feb 1 at 1:47
  • 1
    \$\begingroup\$ Can you show how you did that testing? It helps reviewers if they can see how the function is called (e.g. what's in urldict). \$\endgroup\$ – Toby Speight Feb 1 at 8:48
2
\$\begingroup\$

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

<!doctype html>
<title>SSL Status page</title>
<table>
    {% for row in ssl_connections %}
    <tr>
        {% for item in row %}
        <td>{{ item }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>

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

\$\endgroup\$
  • \$\begingroup\$ This pretty much looks exactly what I want. Surely it's better than my current setup! \$\endgroup\$ – james6125 Feb 1 at 15:19
1
\$\begingroup\$

Usability

The two most common forms of colour-blindness are red-green and green-blue. Throughout this design, you're using green as an indicator colour and expecting users to be able to distinguish it from red or blue.

Instead of baking the specific colours into the code, I think you should use classes, such as

<tr class="no-data"><td>....</td></tr>
<tr class="expiring"><td>....</td></tr>

Then we can provide a stylesheet (or choice of stylesheet, if the UA supports it)

tr.no-data { background: white; color: blue; } /* FIXME - green/blue blindness */
tr.expiring { background: white; color: orange; } /* FIXME - red/green blindness */

Note that whenever we specify foreground colour in CSS, we should also specify a background with the same selector, and vice versa. This is so that other rules in the cascade (including UA rules) are not partially overridden. That's how bad sites end up with black-on-black or other unreadable combinations.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.