tags:
categories:
@blue.route('/addnews/')
def add_news():
news = News()
news.n_title = "小搞怪 %d" % random.randrange(1000)
news.n_content = "社会福利 %d" % random.randrange(100)
db.session.add(news)
db.session.commit()
return '添加新闻列表成功'
@blue.route('/getnews/')
def get_news():
news_list = News.query.all()
return render_template('news_list.html', news_list=news_list)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>News listtitle>
head>
<body>
<h2>今日新闻h2>
<ul>
{% for news in news_list %}
<li>{{ news.n_content }}li>
{% endfor %}
ul>
body>
html>
import requests
def get_data():
response = requests.get("http://127.0.0.1:5000/getnews/")
print(response.content.decode("utf-8"))
if __name__=="__main__":
get_data()
<html lang="en">
<head>
<meta charset="UTF-8">
<title>News listtitle>
head>
<body>
<h2>今日新闻h2>
<script type="text/javascript">
document.write("{{ news_content|safe }}")
script>
body>
html>
<ul>{% for news in news_list %}<li>{{ news.n_content }}li>{% endfor %}ul>
# views.py
@blue.route('/getnews/')
def get_news():
news_list = News.query.all()
news_content = render_template("news_content.html", news_list=news_list)
return render_template('news_list.html', news_content=news_content)
function base64Decode(input) {
_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
_utf8_decode = function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
};
output = _utf8_decode(output);
return output;
}
function showDeHtml(J1) {
return base64Decode(
(base64Decode(J1)["\x72\x65\x70\x6c\x61\x63\x65"]('\x43\x48\x4b\x61\x32\x47\x46\x4c\x31\x74\x77\x68\x4d\x44\x68\x45\x5a\x56\x66\x44\x66\x55\x32\x44\x6f\x5a\x48\x43\x4c\x5a\x6b', ''))
["\x72\x65\x70\x6c\x61\x63\x65"]('\x70\x4f\x71\x33\x6b\x52\x49\x78\x73\x32\x36\x72\x6d\x52\x74\x73\x55\x54\x4a\x76\x42\x6e\x39\x5a', '')
)
}
/**
*
* base64Decode(
* base64Decode(content)['replace']('CHKa2GFL1twhMDhEZVfDfU2DoZHCLZk',''))
* ['replace']('pOq3kRIxs26rmRtsUTJvBn9Z','')
*
* base64Decode( base64Decode(content).replace('CHKa2GFL1twhMDhEZVfDfU2DoZHCLZk', '').replace('pOq3kRIxs26rmRtsUTJvBn9Z', '') )
*
*/
import base64
@blue.route('/getnews/')
def get_news():
news_list = News.query.all()
news_content = render_template("news_content.html", news_list=news_list)
# 字符串转二进制 加密后成二进制在转成字符串
encode_content = base64.standard_b64encode(news_content.encode("utf-8")).decode("utf-8")
print(encode_content)
# 拼串 加入和前端约定好的前后字符串
add_content_encode_content = "CHKa2GFL1twhMDhEZVfDfU2DoZHCLZk" + encode_content + "pOq3kRIxs26rmRtsUTJvBn9Z"
print(add_content_encode_content)
encode_content_twice = base64.standard_b64encode(add_content_encode_content.encode("utf-8")).decode("utf-8")
print(encode_content_twice)
return render_template('news_list.html', news_content=news_content, encode_content_twice=encode_content_twice)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>News listtitle>
<script type="text/javascript" src="{{ url_for('static', filename='js/show.js') }}">script>
head>
<body>
<h2>今日新闻h2>
<script type="text/javascript">
/*document.write("{{ news_content|safe }}");*/
document.write(showDeHtml("{{ encode_content_twice|safe }}"));
script>
body>
html>
@blue.route('/getshow/')
def get_show():
import os, time
from flask import request
from App.settings import BASE_DIR
# 接收前端的时间参数
t = request.args.get("t")
print(t)
# python中以秒为计数单位,其他语言以毫秒计数单位
c = time.time() * 1000
# 捕获异常(防止手动乱输入)
try:
t = int(t)
except:
return "2"
# 1秒之内请求可以正常返回js
if c > t and c - t < 1000:
with open(os.path.join(BASE_DIR, "static\js\show.js"), 'r') as file:
js_content = file.read()
print(js_content)
return js_content
else:
return "1"
<html lang="en">
<head>
<meta charset="UTF-8">
<title>News listtitle>
<script type="text/javascript">
// 这个在前台控制台Elements中才能看见 source中看不到
document.write('