[Share Experiences] 使用 python3 http.server 做简单的WEB服务
Tofloor
poster avatar
酷谷的谷子
deepin
2023-07-02 03:50
Author

负载不高的都可以用来替代 Nginx

起动 ./PythonWww start 停止 ./PythonWww stop

访问 ip:28888

把下面代码保存到PythonWww

#!/bin/bash
INPUT="$1"
#------------------------------
# 引入指定版本 python3 环境变量
#------------------------------
#export MYpython='/opt/Python-3.10.12'
#export PATH=$MYpython/bin:$PATH
python3 --version
#------------------------------
# 清华源
# pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
#------------------------------
# 获取本地IP
#------------------------------
MY_IP=`ip addr|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|sed -n 1p`
#起动的端口
MY_PORT='28894'
APP_NAME='python3'
python3_run=`lsof -i :${MY_PORT} |grep "$USER" |awk '{print $1}'`
#------------------------------
# 创建网站简单实例
#------------------------------
#保存网站路径
MY_WWW_DIR="$HOME/WWW-DATA"
mkdir -p ${MY_WWW_DIR}
echo "0123456789" > ${MY_WWW_DIR}/index.html
#------------------------------
# 起动与停止
#------------------------------
startX='start'
if [[ ! X${INPUT} = X${startX} ]];then
#------------------------------
# 停止
#------------------------------
   stopX='stop'
   if [[ ! X${INPUT} = X${stopX} ]];then
     echo "参数错误"
     echo '---------------------------'
     echo "起动 ./PythonWww start"
     echo '---------------------------'
     echo "停止 ./PythonWww stop"
     echo ""
   else
     MYkill=`lsof -i :${MY_PORT} |grep "$USER" |awk '{print $2}'`
     kill -9 ${MYkill}
     echo "${python3_run}端口${MY_PORT}的进程以杀死"
   fi

else
#------------------------------
# 起动
#------------------------------
  if [[ ! X${APP_NAME} = X${python3_run} ]];then 
    echo '正在起动 python3 http.server'
    nohup python3 -m http.server -b ${MY_IP%%/*} ${MY_PORT} \
    --directory ${MY_WWW_DIR} > $HOME/PythonWww.log 2>&1 &
    echo "网址 http://${MY_IP%%/*}:${MY_PORT}"
    #用来替代一个回车
    yes | echo "OK"
  else
    echo '已起动 python3 http.server'
    echo "网址 http://${MY_IP%%/*}:${MY_PORT}" 
  fi
fi

exit 0

解释代码的作用 ./PythonWww abc 为了方便使用给这个传递的参数使用一个变量名称 INPUT

#!/bin/bash
INPUT="$1"
#表示 可以在执行脚本 外添加一个参数
#比如这样 ./PythonWww abc 其中abc就是第一个参数的值
#可以打印一下
echo "${INPUT}"
#会显示 abc

通过输入的字符 start 或者 stop 来判断是否启动 还是停止

#如果${INPUT}等于${startX}就执行启动相关命令
#如果${INPUT}不等于${startX}就执行结束进程的命令
startX='start'
if [[ ! X${INPUT} = X${startX} ]];then
   echo '结束进程的命令'
else
   echo '启动的命令'
fi

lsof -i :端口号 可以看到这个端口是哪个、程序、用户启动的,以及启动这个端口程序的进程ID

这个脚本也是不严谨就在于端口,root启动的端口在用户下检测不到 需要使用 sudo lsof -i :端口号

获取 ip地址

ip addr|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|sed -n 1p

启动

python3 -m http.server -b IP 端口 --directory 网站路径
Reply Favorite View the author
All Replies
阿尼樱奈奈
Moderator
2023-07-02 03:56
#1

like

Reply View the author
foxbcd
deepin
2023-07-02 04:10
#2
It has been deleted!
foxbcd
deepin
2023-07-02 04:12
#3
It has been deleted!
晚秋(lateautumn)
Moderator
2023-07-02 17:09
#4

虽然我看不太明白,但我真的想明白joy

Reply View the author
酷谷的谷子
deepin
2023-07-02 18:32
#5
晚秋(lateautumn)

虽然我看不太明白,但我真的想明白joy

简单解释了一下脚本,在帖子底部

Reply View the author
xuqi
deepin testing team
2023-07-04 00:36
#6
  • 不错~赞一个~~
Reply View the author
xiaoheishitou
deepin
2023-07-04 17:37
#7

like

内网文件传输 使用这种方式可以避免安装ftp这些东西

Reply View the author