python的类名⼀定要⼤写吗_python⽬录和⽂件名转换成⼩写
或⼤写
需求
linux服务器有⼤⼩写的区分,如果有静态⽂件的路径为驼峰命名
MainStatic/FeatureJs.js
,那么只能通过
MainStatic/FeatureJs.js
访问,⽽⽆法通过
mainstatic/featurejs.js
访问,那么如何让这两个地址都可以访问呢?
思路
先使⽤带lua模块的nginx镜像:nginx-lua来托管我们的静态⽂件,能够实现⼤写的请求转发到⼩写上,然后再使⽤python脚本把静态⽂件全部改成⼩写的形式.
操作步骤
1.环境准备
docker, git, node, python3安装
2. nginx-lua安装
docker run --name master_nginx_2 \
-v /etc/localtime:/etc/localtime \
-v /software/nginx/cert:/etc/nginx/cert \
-v /software/f:/etc/f \
-v /software/nginx/conf.d:/etc/nginx/conf.d \
-v /software/nginx/html:/usr/share/nginx/html \
-v /software/nginx/logs:/var/log/nginx \
--restart=always \
-p 80:80 -p 443:443 \
-d firesh/nginx-lua
3. f配置
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/pes;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 2;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 1k;
gzip_types text/plain text/css application/octet-stream application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png application/x-httpd-php application/vnd.ms-fontobject application/x-font-ttf font/ttf font/opentype font/x-woff image/svg+xml image/x-icon;
include /etc/nginx/conf.d/*.conf;
}
4. nginx配置转发
conf.d⽬录下,⾃⼰的配置⽂件增加lua转发规则
⽐如我的conf.d/01_f
server {
listen 80;
server_name www.lwebapp lwebapp; #域名
}
server {
listen 443 ssl; #SSL协议访问端⼝号为443。此处如未添加ssl,可能会造成Nginx⽆法启动
server_name www.lwebapp lwebapp; # 要解析进来的域名
ssl_certificate /etc/nginx/cert/3114423_lwebapp.pem;
ssl_certificate_key /etc/nginx/cert/3114423_lwebapp.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使⽤此加密套件
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使⽤该协议进⾏配置
ssl_prefer_server_ciphers on;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 2;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 1k;
gzip_types text/plain text/css application/octet-stream application/json application/javascript application/x-javascript
text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png application/x-httpd-php application/vnd.ms-fontobject application/x-font-ttf font/ttf font/opentype font/x-woff image/svg+xml image/x-icon;
location / {
root /usr/share/nginx/html/vue-demo;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
5. python脚本
python⽬录和⽂件名转换成⼩写或⼤写
conver.py
,放置在nginx/html下,后续html下会放置各个项⽬的静态⽂件,所以使⽤conver.py统⼀使⽤脚本处理
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os, sys
def convert(rootdir, optype='l'):
"""
⽬录/⽂件名转换成⼩写或⼤写
:param rootdir: 要转换的根⽬录路径
:param optype: 操作类型(⼩写/⼤写) ⼩写:optype = 'l' ⼤写:optype = 'u',默认转换成⼩写:return:
"""
dirlist = os.listdir(rootdir)
for index, onedir in enumerate(dirlist):
if optype == 'l':
cdir = onedir.lower()
elif optype == 'u':
cdir = onedir.upper()
subdir = os.path.join(rootdir, onedir)
if onedir != cdir: # ⼦⽬录或⽂件名不全为⼩写或⼤写
subcdir = os.path.join(rootdir, cdir)
dirlist[index] = cdir
subdir = subcdir
# print(subdir)
if os.path.isdir(subdir):
convert(subdir, optype)
def help():
print("use: python convert.py 'rootDirPath' {optype:'l'/'u'}")
def main():
if len(sys.argv) == 2:
convert(sys.argv[1])
elif len(sys.argv) == 3:
convert(sys.argv[1], sys.argv[2])
else:
help()
exit(400)
if __name__ == '__main__':
main()
6. 项⽬初始化
前置条件,安装node
# 进⼊/software/vue-demo/repository ⽬录svg的类型有几种
cd /
mkdir -p /software/vue-demo/repository
cd /software/vue-demo/repository
# 拉取初始代码
# 进⼊vue-demo⽬并安装项⽬依赖
cd vue-demo
npm install
# 打包
npm run build
# 新建vue-demo
cd ../../../nginx/html
mkdir vue-demo
# 复制到托管⽬录
cd ../../vue-demo
yes | cp -r repository/vue-demo/dist/* ../nginx/html/vue-demo 7. ⼀键更新脚本
后续github上传代码后,在服务器任意位置使⽤
cd / && cd software/vue-demo && sh deploy_vue_demo.sh
⼀键更新发布
#!/bin/bash
# ⼀键更新部署脚本,⽬录在:/software/vue-demo/下
# pull下来的⽹站代码仓库⽬录
input="repository/vue-demo"
# 挂载到容器的代码⽬录
output="../nginx/html/vue-demo"
cd $input
# 忽略本地,强制从线上更新下来
git fetch --all
git reset --hard origin/master
git pull
# 打包
npm run build
# 备份
cd ../../$output && yes | cp -r * ../../../back/vue-demo

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。