python查詢aqicn的空氣質量數據

#!/data/data/com.termux/files/usr/bin/python
# -*- coding: utf8 -*-

#aqicn空氣質量 #https://aqicn.org/api/
import json,requests,sys

#token從https://aqicn.org/data-platform/token/獲取,只需郵箱即可
token = 'demo'
#預設為當前IP位置,可自行指定基站,幫助 --help
station = "here"

def getAqi(station):    
    aqiUrl = "https://api.waqi.info/feed/"+station+"/?token="+token
    try:
        res = requests.get(aqiUrl)
        a = res.json()
        if a['status']=="ok":
            return a
        else:
            res = requests.get(aqiUrl)
            a = res.json()
            return a
    except:
        return {"status":"net error"}

def searchStation(keyword):
    searchUrl = "https://api.waqi.info/search/?token="+token+"&keyword="+keyword
    try:
        res = requests.get(searchUrl)
        a = res.json()
        res = []
        if a['status']=="ok":
            for s in a['data']:
                resT={}
                resT['uid']=s['uid']
                resT['name']=s['station']['name']
                resT['aqi']=s['aqi']+" ("+s['time']['stime']+")"
                res.append(resT)
            return res
        else:
            return a
    except:
        return {"status":"net error"}
def processAqi(aqiJson):
    s=aqiJson
#     print(json.dumps(s, indent=4))
    res={}
    if s['status']=="ok":
        res['cityName']=s['data']['city']['name']
        res['time']=s['data']['time']['s']
        res['aqi']=s['data']['aqi']
        try:
            res['pm25']=s['data']['iaqi']['pm25']['v']
        except:
            res['pm25']="N/A"
        try:
            res['pm10']=s['data']['iaqi']['pm10']['v']
        except:
            res['pm10']="N/A"
        try:
            res['temp']=s['data']['iaqi']['t']['v']
        except:
            res['temp']="N/A"
        try:
            res['humidity']=s['data']['iaqi']['h']['v']
        except:
            res['humidity']="N/A"
        try:
            res['wind']=s['data']['iaqi']['w']['v']
        except:
            res['wind']="N/A"
    else:
        res=s
    return res

def main():
    global station
    if len(sys.argv)==1:
        res = processAqi(getAqi(station))
    elif len(sys.argv)==2:
        station = str(sys.argv[1])
        if station == "--help":
            res={"@5851":"根據觀測點編號查詢","Nanyang":"根據城市名稱查詢",
                 "here":"根據IP查詢","s shenzhen":"查詢深圳觀測點",}
        else:
            res = processAqi(getAqi(station))
    elif len(sys.argv)==3:
        res = searchStation(str(sys.argv[2]))
    
    print(json.dumps(res, indent=2, ensure_ascii=False))
    
if __name__ == '__main__':
    main()
    

前天迎來如秋後的第一場中度霧霾。

python守護tomcat

下面腳本實現定時檢測tomcat是否還活着,如果死了就啟動它,如果活太久了(24小時)就殺掉再啟動。

#!/data/pythons/p35/bin/python
# -*- coding: utf8 -*-

#定時任務 /etc/crontab
#*/5 * * * * root (/data/pythons/p36/bin/python /data/pythons/scripts/serviceDeamon.py tomcat-8780)

import psutil,time,datetime,subprocess,sys

def log42(logFile,logText):
    ts = int(time.time())
    dt = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    with open("/tmp/"+dt[0:10]+logFile+".log", "a") as myfile:
        myfile.write(dt+" "+logText+"\n")
        
def startByCmdline(cmdlineKeyword): 
    command = 'export JAVA_HOME=/usr/local/java/jdk1.7.0_71;/bin/sh /data/web/'+cmdlineKeyword+'/bin/startup.sh'
    status,output = subprocess.getstatusoutput(command)
    log42('service'+cmdlineKeyword,"started"+ str(status))
    
def killByCmdline(cmdlineKeyword):    
    alive = False
    for proc in psutil.process_iter():
        try:
            pinfo = proc.as_dict(attrs=['pid', 'name','cmdline','create_time','ppid'])
        except psutil.NoSuchProcess:
            pass
        else:
            if any('/'+cmdlineKeyword in s for s in pinfo['cmdline']):
                alive = True
                aliveTime = time.time()-pinfo['create_time']
                if aliveTime > 60*60*24:        
                    log42('service'+cmdlineKeyword,"old enough "+str(aliveTime/60))
                    p = psutil.Process(pinfo['pid'])
                    p.kill()
                    time.sleep(3)
                    startByCmdline(cmdlineKeyword)
                else:
                    log42('service'+cmdlineKeyword,"too young "+str(aliveTime/60))
            else:
                pass
            
    if not alive :
        log42('service'+cmdlineKeyword,"died")
        startByCmdline(cmdlineKeyword)
                
if __name__ == '__main__':
    #'tomcat-8780'
    if str(sys.argv[1]).__contains__('tomcat') :
        killByCmdline(str(sys.argv[1]))
    else:
        log42('service',"wrong param")
        print('param like:tomcat-8780')

在win平台處理自定義協議

在IOS上,Safari可以通過網頁跳轉到snssdk141://detail?id=123456789拉起指定應用並展示內容,但是Safari並不顯示此鏈接。通過添加註冊表和Python可以實現在PC上獲取到這個自定義的協議鏈接。

首先添加註冊表來監聽這種協議,保存下面文本為p.reg運行即可導入:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\snssdk141]
@="\"URL:My Protocol\""
"URL Protocol"=""

[HKEY_CLASSES_ROOT\snssdk141\shell]

[HKEY_CLASSES_ROOT\snssdk141\shell\open]

[HKEY_CLASSES_ROOT\snssdk141\shell\open\command]
@="\"C:\\Users\\42\\AppData\\Local\\Programs\\Python\\Python35\\python.exe\" \"F:\\scripts\\tt.py\" %1"


然後新建F:\\scripts\tt.py內容如下,把鏈接複製到剪切版:

###!C:\Users\42\AppData\Local\Programs\Python\Python35
# coding:utf-8
#snssdk141://detail/?id=123456789
import sys,pyperclip

if __name__ == '__main__':    
    if str(sys.argv[1]) :
        pyperclip.copy(sys.argv[1])
    else:
        pyperclip.copy('fail')

在火狐打開會自動跳轉的網頁,就會彈出選擇框,選擇python運行後就在剪切板里獲取到了snssdk141://detail/?id=123456789。

彎路:火狐並不能添加處理自定義協議的設置。Chrome 58版本在網絡面板可以看到snssdk141://detail/?id=123456789這個鏈接,但是新版Chrome、Chronium和Firefox ESR均無法顯示出網頁中試圖訪問自定義鏈接的請求。requests的作者新開發了個工具叫requests-html,可以支持js和用chrome渲染頁面,及跟蹤網頁跳轉,但是文檔似乎還不完整。

參考:Launching applications using custom browser protocols

Termux Python3.6安裝paramiko

之前寫過一個python腳本通過paramiko使用SSH鏈接越獄後的iPhone執行一些安裝軟件或重啟SpringBoard之類的操作。後來遇到Termux覺得安卓手機也可以試試,不試不知道,一試坑不少,好在最後還是裝好了。

#首先安裝系統的依賴
apt install libffi-dev clang libsodium libsodium-dev openssl-dev libcrypt-dev python-dev
#然後安裝pynacl,直接pip裝會報錯,指定使用系統的sodium庫即可
SODIUM_INSTALL=system
pip install pynacl
#最後再安裝paramiko和python-nmap
pip install paramiko python-nmap

python selenium 常用命令記錄

記錄一些常用命令。

from selenium import webdriver
#代理
from selenium.webdriver.common.proxy import *
#等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
#鍵盤
from selenium.webdriver.common.keys import Keys
#select元素
from selenium.webdriver.support.ui import Select

##配置並開啟火狐
firefox_profile = webdriver.FirefoxProfile()
#語言設置為zh-CN,en-US
firefox_profile.set_preference('intl.accept_languages','zh-CN')
#UA設置為iphone6 plus
firefox_profile.set_preference("general.useragent.override", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
#2關閉瀏覽器圖片
firefox_profile.set_preference('permissions.default.image', 2)
driver = webdriver.Firefox(firefox_profile=firefox_profile)  
#窗口大小
driver.set_window_size(414,736) 
#頁面超時
driver.set_page_load_timeout(50)

#給火狐設置代理
myProxy = "127.0.0.1:1080"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy,
    'ftpProxy': myProxy,
    'sslProxy': myProxy,
    'socksProxy': myProxy,
    'noProxy': '' # set this value as desired
    })
driver = webdriver.Firefox(firefox_profile=firefox_profile,proxy=proxy)
##頁面操作
#打開頁面
try:
    driver.get("https://reportaproblem.apple.com")
except:
    try:
        #長時間沒加載完頁面時,可能是卡在了某個資源,按下Esc停止等待
        driver.find_element_by_xpath('//body').send_keys(Keys.ESCAPE)
    except:
        return 'fail'

#有時鏈接會打開新窗口,切換到新窗口
driver.switch_to_window(driver.window_handles[-1])
#當前頁面鏈接
driver.current_url
#當前頁面標題
driver.title

#等待元素出現
try:
    WebDriverWait(driver, 60).until(lambda the_driver: the_driver.find_element_by_xpath("//iframe[@id='aid-auth-widget-iFrame']").is_displayed())
except:
    pass
##定位元素
#frame
driver.find_element_by_xpath("//iframe[@id='aid-auth-widget-iFrame']")
#ID
driver.find_element_by_id("appleId")
#根據屬性定位元素
captchaInput = driver.find_element_by_xpath("//input[@placeholder='键入图中的字符']")
#獲取輸入框中的值
inputValue = captchaInput.get_attribute('value')
#向輸入框中寫入字符
captchaInput.send_keys("你好")
#清空輸入框
captchaInput.clear()
#按下Tab鍵
driver.find_element_by_xpath('//body').send_keys(Keys.TAB)

capDivImg=driver.find_element_by_xpath("//div[@class='form-cell']//img")
#獲取圖片的鏈接
imgSrc=capDivImg.get_attribute('src')

#點擊包含“繼續”的按鈕
driver.find_element_by_xpath("//button[contains(text(),'继续')]").click()
#根據CSS定位button
driver.find_element_by_xpath("//button[@class='button button-primary last nav-action']")

#點擊值為“步行”的選項
driver.find_element_by_xpath("//option[text()='步行']").click()

#定位一組元素
labels = driver.find_elements_by_css_selector("label.labelClass")
answerInputs = driver.find_elements_by_xpath("//input[@placeholder='答案']")
questions = []
for label in labels:
    questions.append( label.get_attribute('innerHTML') )

#等待包含特定字符的元素
WebDriverWait(driver, 3).until(lambda the_driver: the_driver
           .find_element_by_xpath("//p[@class='subtitle content-item tk-label'][contains(text(),'已超时')]")
           .is_displayed())

#獲取select元素
typeS = Select(driver.find_element_by_id("searchDropdownBox"))
#根據值來選中select元素
typeS.select_by_visible_text('行山')

使用xtrabackup備份mysql數據庫

本文主要記錄下使用percona xtrabackup 2.4來備份mysql數據。最終效果是實現mysql所在主機定時進行全量備份和增量備份並自動刪除舊備份文件。另有一台數據存儲機,定時通過ssh抓取各個mysql主機上的備份文件,保存更長時間後刪除舊文件。

用到的軟件和技術有:xtrabackup(免費的mysql備份程序),qpress(xtrabackup壓縮需要的依賴,據說壓縮比很高),python3.5(ssh的遠程抓取,因為我不會bash)。

mkdir -p /root/42/script
cd /root/42/script
#安裝qpress
wget http://www.quicklz.com/qpress-11-linux-x64.tar
tar xf qpress-11-linux-x64.tar
mv qpress /usr/local/bin/
#centos6安裝xtrabackup
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
yum install libev -y
yum install percona-xtrabackup-24 -y
#新建backup.sh(來源未知)內容如下:
#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
 
BACKUP_BASE_DIR="/data/backup/xtrabackup"
INC_BASE_LIST="${BACKUP_BASE_DIR}/inc_list.txt"
XTRABACKUP_PATH="/usr/bin/innobackupex"
 
MYSQL_CNF="/etc/my.cnf"
MYSQL_HOSTNAME=127.0.0.1
MYSQL_USERNAME=root
MYSQL_PASSWORD=""
 
LOCK_FILE="/tmp/innobackupex.lock"
THREAD=3
 
mkdir -p ${BACKUP_BASE_DIR}
CURRENT_BACKUP_PATH="${BACKUP_BASE_DIR}/$(date +%F_%H-%M)"
[[ -d ${CURRENT_BACKUP_PATH} ]] && CURRENT_BACKUP_PATH="${BACKUP_BASE_DIR}/$(date +%F_%H-%M-%S)"
 
print_help(){
    echo "--------------------------------------------------------------"
    echo "Usage: $0 full | inc | help               "
    echo "--------------------------------------------------------------"
    exit 1
}
 
[[ $# -lt 1 || "$1" == "help" ]] && print_help
 
[[ -f "$LOCK_FILE" ]] && echo -e "Usage: rm -f $LOCK_FILE\nUsage: chattr -i $LOCK_FILE && rm -f $LOCK_FILE" && exit 1
 
FullBackup(){
    touch $LOCK_FILE
    chattr +i $LOCK_FILE
    local rc=0
    ${XTRABACKUP_PATH} \
    --defaults-file=${MYSQL_CNF} \
    --user=${MYSQL_USERNAME} \
    --password=${MYSQL_PASSWORD} \
    --host=${MYSQL_HOSTNAME} \
    --compress \
    --compress-threads=${THREAD} \
    --parallel=${THREAD} \
    --no-timestamp ${CURRENT_BACKUP_PATH} > ${CURRENT_BACKUP_PATH}_full.log 2>&1
    grep ".*\ completed\ OK\!" ${CURRENT_BACKUP_PATH}_full.log > /dev/null 2>&1
    if [ $? -ne 0 ];then
        rc=1
        [[ -d ${CURRENT_BACKUP_PATH} && $(pwd) != "/" ]] && rm -rf ${CURRENT_BACKUP_PATH}
    else
        echo "NULL|${CURRENT_BACKUP_PATH}|full" >> ${INC_BASE_LIST}
        [[ -d ${CURRENT_BACKUP_PATH} && $(pwd) != "/" ]] && chattr +i ${CURRENT_BACKUP_PATH} || rc=1
    fi
    chattr -i ${LOCK_FILE}
    rm -f $LOCK_FILE
    chattr +a ${INC_BASE_LIST}
    return $rc
}
 
IncBackup(){
    touch $LOCK_FILE
    chattr +i $LOCK_FILE
    local rc=0
    PREV_BACKUP_DIR=$(sed '/^$/d' ${INC_BASE_LIST} | tail -1 | awk -F '|' '{print $2}')
    ${XTRABACKUP_PATH} \
    --defaults-file=${MYSQL_CNF} \
    --user=${MYSQL_USERNAME} \
    --password=${MYSQL_PASSWORD} \
    --host=${MYSQL_HOSTNAME} \
    --compress \
    --compress-threads=${THREAD} \
    --no-timestamp --incremental ${CURRENT_BACKUP_PATH} \
    --incremental-basedir=${PREV_BACKUP_DIR} > ${CURRENT_BACKUP_PATH}_inc.log 2>&1
    grep ".*\ completed\ OK\!" ${CURRENT_BACKUP_PATH}_inc.log > /dev/null 2>&1
    if [ $? -ne 0 ];then
        rc=1
        [[ -d ${CURRENT_BACKUP_PATH} && $(pwd) != "/" ]] && rm -rf ${CURRENT_BACKUP_PATH}
    else
        echo "${PREV_BACKUP_DIR}|${CURRENT_BACKUP_PATH}|inc" >> ${INC_BASE_LIST}
        [[ -d ${CURRENT_BACKUP_PATH} && $(pwd) != "/" ]] && chattr +i ${CURRENT_BACKUP_PATH} || rc=1
    fi
    chattr -i ${LOCK_FILE}
    rm -f $LOCK_FILE
    chattr +a ${INC_BASE_LIST}
    return $rc
}
 
## 全量備份
if [ "$1" == "full" ];then
    FullBackup
fi
 
## 增量備份
if [ "$1" == "inc" ];then
    ## 若全量備份不存在,則執行全量備份
    if [[ ! -f ${INC_BASE_LIST} || $(sed '/^$/d' ${INC_BASE_LIST} | wc -l) -eq 0 ]];then
        FullBackup
    else
        IncBackup
    fi
fi
 
## 刪除22天前的備份
if [[ -d ${BACKUP_BASE_DIR} && $(pwd) != "/" ]];then
    find ${BACKUP_BASE_DIR} -name "$(date -d '22 days ago' +'%F')_*" | xargs chattr -i
    find ${BACKUP_BASE_DIR} -name "$(date -d '22 days ago' +'%F')_*" | xargs rm -rf
fi

#可能需要修改backup.sh中的如下參數
BACKUP_BASE_DIR="/data/backup/xtrabackup"
XTRABACKUP_PATH="/usr/bin/innobackupex"
 
MYSQL_CNF="/etc/my.cnf"
MYSQL_HOSTNAME=127.0.0.1
MYSQL_USERNAME=root
MYSQL_PASSWORD=""
#給腳本添加執行權限
chmod 755 /root/42/script/backup.sh
#执行一次全量备份
/root/42/script/backup.sh full
#如果沒有錯誤信息,就可以看到BACKUP_BASE_DIR下生成了備份文件
#檢查下cron是否啟動
service crond status
#如果未啟動則將cron設置為開機啟動並手動啟動下
chkconfig crond on
service crond start
#添加定時任務
crontab -e
#在底部新增內容
## 每周六凌晨3:30一次全量备份
## 每周二、四、日的凌晨3:30点执行增量备份
30 3 * * 6 /root/42/script/backup.sh full
30 3 * * 2,4,7 /root/42/script/backup.sh inc

黑屏

我的這塊戴爾S2340Mc屏,黑色時完全就是一面鏡子!為了當鏡子用,我需要一塊黑色。另外div中height設1000%是不行,參考height: 100% not working

浮動無邊黑屏

#!/usr/bin/python3
# -*- coding: utf8 -*-

#dnf install python3-matplotlib
#pip install matplotlib,fire

#指定起始坐標xy、寬度、高度和顏色,來畫一個填充好的矩形
#為了遮擋盜版電影頂部和底部的賭城廣告

import fire,matplotlib

def drawRectangle(x='0',y='0',width='400',length='400',color='black'):
    #"640x400+0+200"
    conf = str(width) + 'x' + str(length) + '+' + str(x) + '+' + str(y)
    # make sure Tk backend is used
    matplotlib.use("TkAgg")  
    import matplotlib.pyplot as plt
    # turn navigation toolbar off
    plt.rcParams['toolbar'] = 'None'
    plt.rcParams['figure.facecolor'] = color
    # create a figure and subplot
    fig, ax = plt.subplots(figsize=(2,2))
    #remove margins
    fig.subplots_adjust(0,0,1,1)
    # turn axes off
    ax.axis("off")
    # show image
    # im = plt.imread("black.png")
    # ax.imshow(im)
    
    # remove window frame
    fig.canvas.manager.window.overrideredirect(1)
    plt.get_current_fig_manager().window.wm_geometry(conf)
    plt.show()
    
#drawRectangle('10','980')

if __name__ == '__main__':
    fire.Fire(drawRectangle)
    
#默認在屏幕左上角生成一個400*400的黑屏
python drawRectangle.py
#在左下角(0,980)生成一個128*72的紅屏
python drawRectangle.py 0 980 128 72 red