import subprocess import hmac import hashlib import base64 from sanic.response import text from sanic import Blueprint from git import Repo # 路由藍(lán)圖 hook_blue = Blueprint('hook_blue') @hook_blue.route('/hook/kaifa', methods=["POST"]) async def kaifa(request): timestamp = request.headers.get('X-Gitee-Timestamp') # 秘鑰 secret = '**********' secret_enc = bytes(secret.encode('utf-8')) # 把 timestamp+"\n"+密鑰 當(dāng)做簽名字符串 string_to_sign string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = bytes(string_to_sign.encode('utf-8')) # 使用HmacSHA256算法計(jì)算簽名,得到 hmac_code hmac_code = hmac.new(secret_enc, string_to_sign_enc,digestmod=hashlib.sha256).digest() # 將hmac_code進(jìn)行Base64 encode my_sign = base64.b64encode(hmac_code).decode('utf-8') gitee_sign = request.json.get('sign') if my_sign == gitee_sign: gitrepo = Repo("/www/wwwroot/********/") remote = gitrepo.remote() info = remote.pull() return text(str(info)) else: return text('簽名錯(cuò)誤') @hook_blue.route('/hook/ceshi', methods=["POST"]) async def ceshi(request): timestamp = request.headers.get('X-Gitee-Timestamp') # 秘鑰 secret = '*******' secret_enc = bytes(secret.encode('utf-8')) # 把 timestamp+"\n"+密鑰 當(dāng)做簽名字符串 string_to_sign string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = bytes(string_to_sign.encode('utf-8')) # 使用HmacSHA256算法計(jì)算簽名,得到 hmac_code hmac_code = hmac.new(secret_enc, string_to_sign_enc,digestmod=hashlib.sha256).digest() # 將hmac_code進(jìn)行Base64 encode my_sign = base64.b64encode(hmac_code).decode('utf-8') gitee_sign = request.json.get('sign') if my_sign == gitee_sign: # 執(zhí)行的命令 cmd = r'git pull' # cwd指的是某個(gè)進(jìn)程運(yùn)行時(shí)所在的目錄;cwd是“current working directory”的縮寫(xiě) cwd_path = r'/www/wwwroot/********/' process = subprocess.Popen(cmd, shell=True, cwd=cwd_path, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE) process.wait() result = process.returncode if result == 0: return text("git 的拉取:成功") else: return text("git 的拉?。菏?) else: return text('簽名錯(cuò)誤')
/******************************************************************/
conda后臺(tái)運(yùn)行python腳本shell腳本run.sh:
想以www用戶運(yùn)行腳本,記的切換到www用戶,再啟動(dòng)腳本,
./run.sh start
#!/bin/bash
#應(yīng)用入口文件
APP_NAME=/www/wwwroot/python-webhook/main.py
#進(jìn)程關(guān)鍵字
PROCESS_KEYWORD=python-webhook
#使用說(shuō)明,用來(lái)提示輸入?yún)?shù)
usage(){
?? ?echo "Usage: sh run.sh [start|stop|restart|status]"
}
#檢查程序是否在運(yùn)行
is_exist(){
?? ?pid=`ps -ef|grep $PROCESS_KEYWORD|grep -v grep|awk '{print $2}'`
?? ?if [ -z "${pid}" ];then
?? ??? ?return 1
?? ?else
?? ??? ?return 0
?? ?fi
}
#啟動(dòng)方法
start(){
?? ?is_exist
?? ?if [ $? -eq 0 ];then
?? ??? ?echo "${APP_NAME} is already running. pid=${pid}"
?? ?else
?? ? ? ?conda run --name python-webhook nohup python ${APP_NAME} >nohup.out 2>&1 &
?? ?fi?? ?
}
#停止方法
stop(){
? is_exist
? if [ $? -eq "0" ]; then
? ? ps -ef|grep $PROCESS_KEYWORD|grep -v grep|awk '{print $2}'|xargs kill -9
? else
? ? echo "${APP_NAME} is not running"
? fi ?
}
#輸出運(yùn)行狀態(tài)
status(){
? is_exist
? if [ $? -eq "0" ]; then
? ? echo "${APP_NAME} is running. Pid is ${pid}"
? else
? ? echo "${APP_NAME} is NOT running."
? fi
}
#重啟方法
restart(){
? stop
? sleep 5
? start
}
?
#根據(jù)輸入?yún)?shù),選擇執(zhí)行對(duì)應(yīng)方法,不輸入則執(zhí)行使用說(shuō)明
case "$1" in
? "start")
? ? start
? ? ;;
? "stop")
? ? stop
? ? ;;
? "status")
? ? status
? ? ;;
? "restart")
? ? restart
? ? ;;
? *)
? ? usage
? ? ;;
esac
/*****************************************************************/
需要以哪個(gè)用戶來(lái)運(yùn)行python代碼,就切換到哪個(gè)用戶下進(jìn)行安裝:
1. 使用grep www /etc/passwd查看用戶權(quán)限
$ grep www /etc/passwd
www:x:1001:1001::/home/www:/sbin/nologin
可以看出,www是/sbin/nologin禁止登錄的。只要修改這個(gè)模式就可以了
2. 修改模式
$ usermod -s /bin/bash www
3. 再次查看狀態(tài)
$ grep www /etc/passwd
www:x:997:995:www user:/var/cache/www:/bin/bash
4. 然后就可以用su - www切換了
$ su - www
1
5. 恢復(fù)的話改為/sbin/nologin即可
$ usermod -s /sbin/nologin www
開(kāi)始安裝:
miniconda和anaconda下載地址
https://docs.conda.io/en/latest/miniconda.html
Free Download | Anaconda
Centos7.9安裝miniconda
Miniconda是一個(gè) 免費(fèi)的 輕量級(jí)的 conda安裝程序
conda是一個(gè)開(kāi)源的包、環(huán)境管理器,能在同一個(gè)機(jī)器上安裝不同Python版本的軟件包及其依賴,以及在不同Python環(huán)境之間切換
Miniconda只包含conda、Python、pip、zlib等基礎(chǔ)的文件和依賴包
Anaconda不僅包含conda、Python等基礎(chǔ)文件,還包含很多裝好的包,如:numpy、pandas
使用conda install命令可從Anaconda存儲(chǔ)庫(kù)中安裝額外的conda包
下載Miniconda3-latest-Linux-x86_64.sh
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
運(yùn)行.sh
sudo sh Miniconda3-latest-Linux-x86_64.sh
輸入安裝的路徑,如/usr/anconda3
添加/usr/anconda3到系統(tǒng)環(huán)境變量文件/etc/profile文件
sudo vi ?/etc/profile
添加
export PATH=/usr/anconda3/bin:$PATH
激活生效,或重啟
source /etc/profile
檢測(cè)安裝是否成功
conda -V
2、配置conda鏡像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
# optional
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --set show_channel_urls yes
/****************************************/
要卸載 Miniconda,請(qǐng)按照以下步驟進(jìn)行操作:
打開(kāi)終端或命令提示符窗口,確保以管理員權(quán)限運(yùn)行。
根據(jù)你的操作系統(tǒng),執(zhí)行以下命令卸載 Miniconda:
在 Windows 上:
conda install anaconda-clean
anaconda-clean --yes
在 macOS 或 Linux 上:
conda install anaconda-clean
anaconda-clean --yes
在某些 Linux 發(fā)行版中,可能需要在命令前加上 sudo。
確認(rèn)卸載操作。執(zhí)行上述命令后,你將被要求確認(rèn)卸載。請(qǐng)仔細(xì)閱讀提示信息,然后輸入 y 或 yes 確認(rèn)卸載。
刪除 Miniconda 安裝目錄:在終端中執(zhí)行以下命令,將 <miniconda_install_dir> 替換為你的 Miniconda 安裝目錄:
rm -rf <miniconda_install_dir>
注意:請(qǐng)謹(jǐn)慎執(zhí)行此命令,確保你刪除的是正確的安裝目錄。
檢查環(huán)境變量:卸載 Miniconda 后,你可能還需要手動(dòng)刪除與 Miniconda 相關(guān)的環(huán)境變量。在 Windows 上,可以通過(guò) “控制面板” -> “系統(tǒng)和安全” -> “系統(tǒng)” -> “高級(jí)系統(tǒng)設(shè)置” -> “環(huán)境變量” 打開(kāi)環(huán)境變量設(shè)置界面,然后檢查并刪除相關(guān)的環(huán)境變量。在 macOS 或 Linux 上,可以編輯 ~/.bashrc 或 ~/.bash_profile 文件,并刪除相關(guān)的路徑配置。
完成上述步驟后,你的系統(tǒng)應(yīng)該已成功卸載 Miniconda。請(qǐng)確保在卸載之前備份你的數(shù)據(jù),以防萬(wàn)一。
/**************************************************/
創(chuàng)建虛擬環(huán)境:
conda create -n python-webhook python=3.11
后臺(tái)運(yùn)行python腳本:
conda run --name python311-venv nohup python main.py >nohup.out 2>&1 &
/*************************************************************/
conda可以配合其他python包管理工具一起使用,比如pipenv或者poetry,
conda安裝好某個(gè)python版本的虛擬環(huán)境后,就在這個(gè)虛擬環(huán)境運(yùn)行其他包管理工具即可,文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-687598.html
比如poetry包管理的項(xiàng)目,虛擬環(huán)境安裝好poetry工具后,運(yùn)行poetry install即可安裝好項(xiàng)目需要的依賴包,然后啟動(dòng)項(xiàng)目即可文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-687598.html
到了這里,關(guān)于python conda實(shí)踐 sanic框架gitee webhook實(shí)踐的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!