2022年3月30日 星期三

自建 DDNS 轉址

由於 HiNet 只提供一個固定 IP, 只能給一個路由器使用, 如果另一個路由器用 PPPOE 取得浮動 IP, 可透過這個固定 IP 下的網站來進行轉址. 本例中, 浮動 IP 端使用樹莓派當伺服器, 使用 python3 環境, 固定 IP 端為一般 PC, 安裝 xampp (apache+php+mysql).

PC端在 htdocs 下建立一個 smartcenter 目錄, 放3個 php 檔

set_ip.php:  設定 ddns ip

<?php

        $host =  htmlspecialchars($_GET["HOST"]);

        $ip = htmlspecialchars($_GET["IP"]);

        $filename = "ip.".$host; 

        $file = fopen($filename,"w");

        fwrite($file,$ip);

        fclose($file);

?>

 get_ip:php: 回傳 ddns ip

<?php

        $host =  htmlspecialchars($_GET["HOST"]);

        $filename = "ip.".$host;

        if (file_exists($filename))

        {

                $file = fopen($filename,"r");

                $ip = fread($file,filesize($filename));

                fclose($file);

        } else {

                $ip="0";

        }

        echo '{"IP":"'.$ip.'"}';

?>

 index.php: 轉址

<?php

        $filename = "ip.smartcenter";

        if (file_exists($filename))

        {

                $file = fopen($filename,"r");

                $ip = fread($file,filesize($filename));

                fclose($file);

        } else {

                $ip="0";

        }

       

    header('Location: http://'.$ip);

?>

樹莓派的 set_ddns_ip.py 程式: 先取得外部 ip, 呼叫 set_ip.php 進行設定, 再呼叫 get_ip.php 檢查結果:

#!python3

import requests

r =requests.get('https://api.myip.com/')

print(r.status_code)

print(r.text)

 

#print(r.json())

my_ip=r.json()['ip']

print('my_ip="'+my_ip+'"')

 

ddns_server='http://自己的網址'

host='smartcenter'

args = {'HOST':host,'IP':my_ip}

r = requests.get(ddns_server+'/smartcenter/set_ip.php',params=args)

#print(r.status_code)

#print(r.text)

 

 

args = {'HOST':host}

r = requests.get(ddns_server+'/smartcenter/get_ip.php',params=args)

#print(r.status_code)

registered_ip=r.json()['IP']

print('registered_ip="'+registered_ip+'"')

 

if(registered_ip!=my_ip):

    print("Bad")

else:

    print("OK");

 建立一個 /set_ddns_ip.sh

 

 然後只要設定 crontab 每分鐘自動更新即可:

# m h  dom mon dow   command

* * * * * /home/pi/set_ddns_ip.sh

 

 瀏覽器只要使用原先的網址, 例如 http://自己的網址/smartcenter, 即可自動轉址

 

 

 

2021年10月11日 星期一

[Python] 計算RMT檔案目錄下的檔案數

 #!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 11 08:55:28 2021

@author: ghosty
"""
from os import listdir
from os.path import isfile, isdir, join
from  datetime import datetime, timedelta

# 指定要列出所有檔案的目錄
root_path = "E:\\RMT Processing\\Source\\"

def getFileCount(path):
    count = 0
    files = listdir(path)
    #print(files)
    for f in files:
        #print(f)
        fullpath = join(path, f)
        if isfile(fullpath):
            count += 1
    return count
            
    
start_data = datetime(2021, 10, 2)    
stop_data = datetime(2021, 10, 10)

search_date = start_data
while (search_date <= stop_data):
    for hour in range(24):
        searth_path = root_path + search_date.strftime("%Y-%m-%d") + '\\{:0>2}'.format(hour)
        count = getFileCount(searth_path)   
        if (count<700 or count>720) :
            print(searth_path+': filecount=', count)
    search_date += timedelta(days=1)

2021年10月10日 星期日

[HTML] bootstrap5 與 chart.js (v3)

最近預計會有一些製作網頁的需求, 學著用 bootstrap5 chart.js, 碰到一些問題, 上網搜尋解答, 解決後做個紀錄

2021年9月29日 星期三

[PHP] 掃描子目錄, 自動建立連結標籤

掃描子目錄, 自動建立連結標籤, 例如將  startbootstrap 系列的範例解開放在子目錄下:

以下是 index.php 的內容

2021年7月31日 星期六

[Python] 股市勝率與獲利/虧損組合計算

最近讀到股市投資 "超級績效" 系列, 裡面有一段關於勝率與獲利/虧損組合的計算, 提到虧損造成的傷害具有幾何累績性, 其實獲利也是幾何累積的, 為釐清其中的比例關係, 確定作者的觀點是如何計算出來的, 因此寫了一段程式來驗證看看.