0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ハム_カウンター python de 画像の来訪者カウンター.cgi

Posted at

Hamster.gif(png) をつなげて表示する。

capture_250730_064703.png
こんなやつ。。。
画像をあげておく。

0.gif
1.gif
2.gif
3.gif
4.gif
5.gif
6.gif
7.gif
8.gif
9.gif

昔、perl で実装していたが、カウンターの桁数まで行ったらカウントが上位に拡大表示されるか分からないので、python で作り直しました。
左の大きいのは0埋めも最低桁数も決められる。右の小さいのが一日のカウント数。
さらに、日付も出してみた。簡単に出さなく出来る。today を消しておけばいいのや。

では、全体から。

python hamu_counter.py
#! c:/python3_12/python -X utf8
# -*- coding: utf-8 -*-
# ranning python3.12.0
# this script is for Apache CGI
# http://localhost/sample_PY/hamu_counter.py

import os
import sys
def display():
    print("Content-type: text/html\n\n")
    print("<!DOCTYPE html>")
    print("<html lang='ja'>")
    print("<head>")
    print("<meta charset='UTF-8'>")
    print("<title>python Hamster Counter</title>")
    print("</head>")
    print("<body>")
    print("<h1>python Hamster Counter</h1>")
    print("<p>#! c:/python3_12/python -X utf8</p>")
    filename = './user/count_py.txt'
    # カウントアップ
    from datetime import datetime
    today = datetime.now()
    weekday_name = today.strftime('%A')
    cou_str = str(0) + "<>" + weekday_name + "<>" + str(0) + "<>"
    #directory = os.path.dirname(os.path.abspath(__file__))
    #filename = os.path.join(directory, filename)
    print("<p>ファイル名: " + filename + "</p>")
    if os.path.exists(filename):
        print("<p>ファイルは存在します。</p>")
        with open(filename, "r") as file:
            cou_str = file.read()
            file.close()
    else:
        print("<p>ファイルは存在しません。新規作成します。</p>")
        cou_str = str(0) + "<>" + weekday_name + "<>" + str(0) + "<>"
        with open(filename, "w") as file:
            file.write(cou_str + "\n")
            file.close()
        if os.path.exists(filename):
            print("<p>ファイルを新規作成しました。</p>")
        else:
            print("<p>ファイルの新規作成に失敗しました。</p>")
            #return
    with open(filename, "r") as file:
        cou_str = file.readlines()
        file.close()
    print("<p>ファイル内容: " + cou_str[0] + "</p>")
    # cou_str = cou_str[0].strip()  # 改行を削除
    parts = cou_str[0].split("<>")
    count = int(parts[0])
    cou_wday = parts[1]
    to_day = int(parts[2])
    count += 1
    print("<p>if "+weekday_name+" == "+cou_wday+":</p>")
    if weekday_name != cou_wday:  # 今日の日付とカウントの値が違う場合:
        to_day = 0
        cou_wday = weekday_name
    to_day += 1  # 今日の日付を+ # 今日のカウントを1増やす
    print(str(count) + "<>" + cou_wday + "<>" + str(to_day) + "<>" + "<br>\n")
    with open(filename, "w") as file:
        file.writelines(str(count) + "<>" + cou_wday + "<>" + str(to_day) + "<>" + "\n")
        file.close()
    count = str(count).zfill(5) # カウントを5桁にゼロ埋め以上になると増えていく 先頭に0が1個増えて付いていく5の時6桁 01が最低表示。    
    print("<p>今日の日付: " + str(today) + "</p>")
    to2_day = str(to_day).zfill(2)+" "+str(today)  # 今日のカウントを2桁にゼロ埋め
    print("<p>曜日: " + str(weekday_name) + "</p>")
    print("<p>カウントの値: " + str(count) + "</p>")
    disp_cunt = "<img src='imge/0.gif' alt='0'>"
    len_cou =len(count)
    for i in range(len_cou):
        char = str(count)[i:i+1]#"1" i=0 ?strto
        disp_cunt = disp_cunt + "<img src='imge/" + char + ".gif'>"       
    disp_cunt = disp_cunt + "<b><font color='blue'>day</font></b>"
    len_today = len(str(to2_day))
    for ii in range(len_today):
        char = str(to2_day)[ii:ii+1]#"2" 
        disp_cunt = disp_cunt + "<img src='imge/" + char + ".gif' width='14px' height='22px'>"
    print("<h1>python 管理画面</h1>")
    version_str = sys.version
    print(version_str)
    print(disp_cunt)
    print("</body>")
    print("</html>")
# end def display()
display()

exit()

一番上は、zipと上書きimsでpython_12 ディレクトリにして呼びやすくしたやつ。-X utf8 は起動をutf8でやるってやつ。
Apache2.2の骨董品だが *.pl が動くようにhttpd.confg 足してある。
機会があったら触れます。そんな、難しい物でもない。
localhost で開発確認しています。
使う機会があったら、使ってみてね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?