[root@lvs-nginx1 ~]# wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz [root@lvs-nginx1 ~]# tar -xf Python-3.7.3.tgz [root@lvs-nginx1 ~]# mkdir /usr/local/python3.7.3 [root@lvs-nginx1 ~]# cd Python-3.7.3 [root@lvs-nginx1 ~]# ./configure --prefix=/usr/local/python3.7.3 --enable-optimizations [root@lvs-nginx1 ~]# make && make install [root@lvs-nginx1 Python-3.7.3]# ln -s /usr/local/python3.7.3/bin/python3.7 /usr/bin/python3.7 [root@lvs-nginx1 Python-3.7.3]# python3.7 Python 3.7.3 (default, Nov 7 2019, 16:07:52) [GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit()
[root@k8s-es7 python]# /usr/local/python3.7/bin/django-admin startproject getoslogs [root@k8s-es7 python]# cd getoslogs/ [root@k8s-es7 getoslogs]# python manage.py startapp getlogs ##建立app [root@k8s-es7 getoslogs]# python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying sessions.0001_initial... OK [root@k8s-es7 getoslogs]# python manage.py createsuperuser Username (leave blank to use 'root'): root Email address: 547253687@qq.com Password: Password (again): This password is too short. It must contain at least 8 characters. This password is too common. This password is entirely numeric. Bypass password validation and create user anyway? [y/N]: y Superuser created successfully.
此時在getoslogs/settings.py裏面INSTALLED_APPS添加剛剛建立的getlogs,將ALLOWED_HOSTS替換成ALLOWED_HOSTS = ['*'],容許全部人訪問,這時候就建立好了項目和app。啓動css
python manget.py runserver 0.0.0.0:5566,這時候django已經正常啓動,訪問以下圖html
這個腳本很簡單,就是兩個函數,listfile函數用來獲取前端輸入的時間,oss api來獲取到該時間下的備份日誌文件,返回給前端。前端
filejiedong這個函數用來獲取前段勾選的文件,解凍以及下載文件python
[root@k8s-es7 getoslogs]# cd /home/pb/python/test/ [root@k8s-es7 test]# ll 總用量 0 [root@k8s-es7 test]# cat listdir.py #!/usr/local/bin/python3.7 # -*- coding: utf-8 -*- import oss2,time,sys; from flask import Flask,send_file from django.shortcuts import render from django.http import HttpResponse; sys.path.append('/home/pb/python/getoslogs/getlogs'); import views; auth = oss2.Auth('yourAccessKeyId','yourAccessKeySecret'); bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai-finance-1-internal.aliyuncs.com', 'yourBucketName'); def listfile(date_time): data = []; data1 = []; for obj in oss2.ObjectIterator(bucket, prefix = date_time): data.append(obj.key) for x in range(len(data)): if "tar.gz" in data[x]: data1.append(data[x]) return data1; def filejiedong(objectname): meta = bucket.head_object(objectname); if "expiry-date" not in str(meta.resp.headers): bucket.restore_object(objectname); while True: time.sleep(3); meta = bucket.head_object(objectname); if "expiry-date" in str(meta.resp.headers): requesturl=(bucket.sign_url('GET',objectname,36000)); return requesturl; break; else: continue; else: requesturl=(bucket.sign_url('GET',objectname,36000)); return requesturl;
而後去前端頁面準備一個簡單的html頁面linux
[root@k8s-es7 getlogs]# mkdir /home/pb/python/getoslogs/getlogs/templates [root@k8s-es7 getlogs]# cd /home/pb/python/getoslogs/getlogs/templates [root@k8s-es7 templates]# cat listfile.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>金融雲日誌下載</title> <link rel="stylesheet" href="../../static/css/layui.css" media="all"> </head> <body> <h1>金融雲日誌下載</h1> <h2>請根據實際狀況填寫如下類容</h2> <form method="post" action="/listfile/"> <!-- <input type="text" name="file_date" placeholder="日期"> --> <div class="layui-inline"> <!-- 注意:這一層元素並非必須的 --> <input type="text" class="layui-input" id="test1" name="file_date" placeholder="yyyy-MM-dd"> </div> <br> {{ error }} <br> <button id="btn" type="submit" class="layui-btn layui-btn-sm">查詢</button> {% csrf_token %} <!-- 標籤添加CSRF令牌,這是由於django針對CSRF(跨站請求僞造)有保護措施,沒有這句話就是403 --!> </form> <form method="post" action="/listfile/"> <button id="btn" type="submit" class="layui-btn layui-btn-normal layui-btn-sm">解凍</button> {% for url in result %} </br> <input type="checkbox" value="{{ url }}" name="file_jiedong"> <a> {{url}} </a> {% endfor %} {{ error }}<br> {% csrf_token %} </form> {% for url in result1 %} </br> <a href={{url}}>{{url}}</a> {% endfor %} {{ error }}<br> {% csrf_token %} <script src="../../static/layui.js"></script> <script> layui.use('laydate', function() { var laydate = layui.laydate; //執行一個laydate實例 laydate.render({ elem : '#test1' //指定元素 }); }); </script> </body> </html>
有了頁面以後,就須要建立一個url指向這個這個頁面,修改nginx
[root@k8s-es7 templates]# vim /home/pb/python/getoslogs/getoslogs/urls.py from django.contrib import admin from django.urls import path from getlogs import views #將createyaml這個app的views變量插進來 urlpatterns = [ path('admin/', admin.site.urls), path(r'listfile/', views.listfile), #將這個listfile url指向getlogs/views.py裏面的listfile函數 ]
編寫view.py裏面的listfile函數django
提早安裝好oss2模塊 pip install aliyun-python-sdk-core-v3
flask
[root@k8s-es7 getlogs]# cat views.py # -*- coding:utf-8 -*- from django.shortcuts import render from django.http import HttpResponse; import subprocess; import os,oss2; import sys; from django.shortcuts import render_to_response from django.template import RequestContext sys.path.append('/home/pb/python/test'); import listdir; def listfile(request): if request.method == 'POST': file_date = request.POST.get('file_date', '') file_jiedong = request.POST.getlist('file_jiedong', '') env = request.POST.get('env') if file_date: result = listdir.listfile((file_date).strip()); context = {'result': result} return render(request,'listfile.html', context) else: pass; if file_jiedong: context = for_file(file_jiedong); return render(request,'listfile.html', context) else: pass; else: return render(request,'listfile.html'); def for_file(file_jiedong): result_file = [] for i in file_jiedong: result1 = listdir.filejiedong((i).strip()); result_file.append(result1) context = {'result1': result_file} return context; #def add_dict(list_test): # context = {} # a = 0 # for i in list_test: # a = a + 1 # context["result"+str(a)] = i # return context
還須要在getoslogs/settings.py添加staticvim
STATIC_URL = '/static/' HERE = os.path.dirname(os.path.abspath(__file__)) HERE = os.path.join(HERE, '../') STATICFILES_DIRS = ( # Put strings here, like "/home/html/static" or "C:/www/django/static". # Always use forward slashes, even on Windows. # Don‘t forget to use absolute paths, not relative paths. os.path.join(HERE, 'static/'), )