Wednesday, July 17, 2019

A Simple Persistent Page Counter for Python

I have been playing with Gopherspace (yes, that is still a thing) and Flask-Gopher. I needed a simple visitor counter for Python 3.7 so I can see how many times a page loaded.

Here is the solution I came up with. It creates a text file called counter.dat and stores a number in it.  Each time the code runs, the number in the file increments.

I used pathlib to solve file path differences between Unix and Windows.
I hope it helps you!


from pathlib import Path

#Change path to the Linux or Windows folder where your counter .dat file will live.
data_folder = Path('c:/Users/change_this/Documents/')
counter = data_folder / 'counter.dat'

try:
    with open(counter, mode='r') as f:
        count = int(f.read())+1

    with open(counter, mode='w') as f:
        f.write(str(count))
        f.close()

except:
    print('first run exception fired')
    #create the counter.dat file on first run
    count = 1
    with open(counter, mode='w') as f:
        f.write(str(count))
        f.close()

print('You are visitor number ' + str(count))

Tuesday, March 26, 2019

7Zip Command Line Random File Name

I need to make backups of a database every hour.  I spent a little too much time coming up with my simple solution, so passing it on here to save you the time.

My goal was to run a Windows batch file that will simply pack up certain files into a zip archive, give the archive a random name so it won't overwrite other archives, and apply password encryption to the archive.

Here's how I call 7zip from the Windows command line to do what I need.

7z.exe a -tzip -p"Password" %random%.zip *.db

What it means

7z.exe, call 7zip from command line
a,  add files
-tzip, use zip compression
-p"Password",  encrypt using Password as the password.  Yes, you need to use the quotations.
%random%.zip,  pulls a random number from the Windows shell, use it as a file name and append .zip to it.
*.db, pack up all .db files in the folder.

The rest of my project involved copying the encrypted zip file to Dropbox for offsite storage. There are most certainly other perhaps better ways to accomplish this, but maybe none more simple than this. The timestamp on the file makes it simple to spot the most recent file.  In my experience, simple is usually better.

7zip is a fantastic tool capable of doing a lot.  While Googling around, I found posts that focused on formatting date/time for use in a file name.  But in the end, all I really need is a random number.

That's all there is to it!  Now go get some coffee and enjoy the 30 minutes you just saved!

Addendum:

Ok, I wanted to add this here, if only for my own future use. Sathyajith Bhat had a great answer elsewhere that showed me how to put the current date into the archive file name with 7zip. Try this:

7z -tzip -aou -p"Password" "%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4% Backup".zip *.db

This will create a file named something like:     26.03.2019 Backup.zip
Using Bhat's string extraction tip, you can do all kinds of other things.

Monday, February 18, 2019

Logging into the 80's and 90's Online with BBS Services

Back in the 80's and 90's before the widespread adoption of the internet, tech enthusiasts built and used a wide network of computer bulletin board services. To log into a BBS, you first had to purchase a modem to connect your computer to a telephone line.  You had to know the phone number of the BBS service you wanted to reach. Sometimes you could find a list of top BBS's printed in a computer magazine or club newsletter, or in an alternative newspaper of trendy 'zine.

When you attempted to connect to a BBS, your modem would screech and purr as the fastest connection speed was negotiated with the BBS's modem.  If you were lucky, you might get a top speed of 9600 baud, but may have to settle for 2400, 1200, or (horrors!) a slow 300 baud connection.  The slowest connections made downloading even small 5k files a tortuous ideal.

As a haven for hobbiests and enthusiasts, BBS users rarely worried about contracting a computer virus from a download.  There were a wide range of operating systems from C64, MS-DOS, CP/M and others, and a lack of always-on connectivity that took the scale out of launching a computer virus.

My earliest experience with BBS systems began around 1981 with my Osborne 1 computer and 300 baud modem.  It was a way to connect with other nerdy teens all over the city, country and world.  Most BBS's used text, but some found clever ways to present blocky 8-bit ANSI graphics.

Worldwide communication was expensive back then. It would cost a lot of money to connect to a BBS in Europe, a thrill that was reserved only when there was a specific file that I absolutely had to have.  Most global communication took place through the Fidonet.

Fidonet was an electronic mail system that BBS's could utilize. Users would get their very own personal Fidonet addresses and could receive private messages. 

Fidonet also offered public message boards through their Echomail service.  Anything you posted to a public echomail board would eventually weave its way to all of the other BBS systems that carried that board.

I tried to get a clear idea of the state of Fidonet. There are a lot of dead Fidonet websites.  It appears with more effort and outreach I may have been able to find some working Fidonet nodes.  Perhaps someone can update me clearly on the state of Fidonet. I would be cool if it was still operating.

Using echomail was a great way to promote a BBS system. Most of the time, when you posted in echomail, the BBS system where the post originated would include a signature tag line that told the name of the BBS and a phone number for connecting to it. 

Recently, I engaged in a little techno-nostalgia and set out to revisit my old BBS world.  It turns out that there is a small but enthusiastic world of BBS users still around! Many of them can now be accessed by Telenet over the internet.

I used a free app called Putty to Telenet to the BBS services. To use putty, just enter the Host Name and port for the BBS and click "Open."

Use Putty to Connect to a BBS Service.

Of course, you need to find a list of active BBSes before you can connect. A great place to find BBS's is through the Telnet BBS Guide, conveniently accessible over the internet.
Connecting to a BBS over Telnet
Connected to a BBS via Telnet

Soon, you'll be browsing through an entirely new online world that you didn't even know existed.

If you like to time travel and would like to step back and see the earliest days of online communication,  give BBS'ing a try.  The technical threshold is low, and the experience will give you a foundation of appreciation for has been accomplished since.

Have you logged into a BBS?  Post in the comments what you experience was like.  And as always, have fun with it!