Yearly Archives

18 Articles

一個使用Cloudflare API v4的動態DNS腳本

0   13061 轉為簡體

分享一個使用Cloudflare API v4的動態dns腳本(Gist地址本地下載)。

因為11月Cloudflare就要下線v1版本的API了,而Openwrt里的ddns-scripts並沒有支持新版[攤手],所以自己寫了一個。

僅依賴curl(可以改成wget)。支持v4和v6。會請求ip.yooooo.us得到公網IP地址,怕隱私泄露的可以自己搭一個

 

This is a DDNS script using Cloudflare APIv4 written in bash (Gist or Download directly).

I wrote this because Cloudflare is going to remove support for the old API on Nov. this year. And the ddns-scripts package in Openwrt is not yet supporting the new API [攤手].

This script only depends on curl (can be modified to use wget). It will use ip.yooooo.us to query IPv4 and IPv6. You can use this link to set up your own query server.

deluge各種問題導致狀態機抽風

6   9865 轉為簡體

遇到一個問題,設置了deluge分享率和做種時間到一定時間之後自動刪除種子。之前一直很好使的,最近發現到了指定的分享率或者做種時間之後,種子狀態變成了一直Paused或者一直Seeding。發現有這麼幾種問題導致deluge抽風:

狀態存檔的權限不對

是deluged沒有對狀態存檔~/.config/deluge/session.state和~/.config/deluge/state/*的寫權限導致的。

因為自身需求使用

#  start-stop-daemon -S -c user:www-data -k 000 -x /usr/bin/deluged — -d

啟動deluged,deluged具有user的euid和www-data的egid,而~是user:user的。所以就掛了233

直接添加種子下載地址導致一直Paused

deluge可以直接添加種子的http下載地址,取得種子之後自動開始下載,但是這樣有一定幾率產生萬年Paused。

解決辦法是自己下載種子之後返回磁力鏈添加到deluge。

 

Python float和decimal

2   10390 轉為簡體

在Windows上打包Python腳本時遇到一個需求,將浮點數轉成字符數組,比如輸入1.001,得到[‘1′,’0′,’0′,’1’]

一開始想當然地用了:

version = 2.001

v = list(str(int(version * 1000)))

發現好像哪裡不對,得到的v是[‘1’, ‘0’, ‘0’, ‘0’]。想了一想應該是浮點數的精度問題,因為一看version * 1000 = 1000.9999999999999

所以有這麼幾種解決方法:

v = list(str(int(round(version * 1000))))

或者:

v = list(str(version).replace(“,”, “”) + “000”)[:4]

或者用decimal模塊,decimal是固定小數點位數的,用的是十進制乘除法,所以(在設定的位數內)不會產生誤差:

from decimal import getcontext, Decimal

getcontext().prec = 4

v = list(str(Decimal(version) * 1000))

需要注意的是decimal會自動轉換科學計數法,可以用”%d” % Decimal(d)來得到原始值。