Python 7 -- 文件存储数据
上一节总结了一个基本web应用的代码,这一节主要讲用户访问的数据记录在log文件中,并显示在页面上。
这节步骤:
- 按以下目录建好相应的文件夹及内容
webapp
|----vsearch4web.py
|----vsearch.log #自动创建的log文件,用于记录浏览器访问信息。
|----static
| |----hf.css
|----templates
| |----base.html #基模板
| |----entry.html
| |----result.html
| |----Viewlog.html #查看日志文件
相比上一节,需要修改vsearch4web.py来将日志写入log文件,并新增Viewlog.html 来友好的显示结果。
修改后的vsearch4web.py
from flask import Flask, render_template, request, escape
from vsearch import search4letters app = Flask(__name__) # 写日志文件,同一个请求的不同信息以'|'分隔。
def log_request(req: 'flask_request', res: str) ->None:
with open('vsearch.log', 'a') as log:
print(req.form, req.remote_addr, req.user_agent, res, file=log, sep='|') @app.route('/search4', methods=['GET', 'POST'])
def do_search() -> 'html':
phrase = request.form['phrase']
letters = request.form['letters']
title = 'Here are your results:'
results = str(search4letters(phrase, letters))
log_request(request, results)
return render_template('result.html', the_title=title, the_phrase=phrase, the_letters=letters, the_results=results) @app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
return render_template('entry.html', the_title='Welcome to this Web!') @app.route('/viewlog')
def view_the_log() -> str:
contents = []
#读取日志文件,并读取为嵌套列表,便于以表格样式显示结果。
with open('vsearch.log') as log:
for line in log:
contents.append([])
for item in line.split('|'):
contents[-1].append(escape(item))
titles = ('Form data', 'Remote_addr', 'User_agent', 'Results')
return render_template('Viewlog.html',
the_title='View Log',
the_row_titles=titles,
the_data=contents,) if __name__ == '__main__':
app.run(debug=True)
新增的Viewlog.html
{% extends 'base.html' %}
{% block body %}
<h2>{{the_title}}</h2>
<table>
<tr>
{% for row_title in the_row_titles %}
<th>{{row_title}}</th>
{% endfor %}
</tr>
{% for log_row in the_data %}
<tr>
{% for item in log_row %}
<td>{{item}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endblock %}
2. 启动服务器,并访问页面http://127.0.0.1:5000/,输入要查询的英文句子后,点击do it.随后访问http://127.0.0.1:5000/ viewlog.html可以查看到访问信息。
Python 7 -- 文件存储数据的更多相关文章
- Android使用文件存储数据
Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...
- Android开发手记(17) 数据存储二 文件存储数据
Android为数据存储提供了五种方式: 1.SharedPreferences 2.文件存储 3.SQLite数据库 4.ContentProvider 5.网络存储 本文主要介绍如何使用文件来存储 ...
- [ Android 五种数据存储方式之二 ] —— 文件存储数据
关于文件存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的. 文件可用来存放大量数据,如文本.图片.音 ...
- Python 读写文件中数据
1 需求 在文件 h264.txt 中的数据如图1,读入该文件中的数据,然后将第1列的地址删除,然后将数据输出到h264_out.txt中: 图1 h264.txt 数据截图 ...
- PHP格式化(文件)存储数据大小(SIZE)显示
有时候我们需要在网页上显示某个文件的大小,或者是其它数据的大小数字. 这个数字往往从跨度很大,如果以B为单位的话可能是个位,如果1G则长达1073741824的数字,这个时候我们就需要根据大小来格式化 ...
- linux下在用python向文件写入数据时'\n'不起作用
网上翻看一圈,大家都说利用write写数据换行,在linux下用'\n',windows下利用'\r\n',可是尝试了一下,'\n'在windows底下可换行,在linux底下居然不起作用,最后利用' ...
- Python存储数据的方式
在Python开发中,数据存储.读取是必不可少的环节,而且可以采用的存储方式也很多,常用的方法有json文件.csv文件.MySQL数据库.Redis数据库以及Mongdb数据库等. 1. json文 ...
- Android开发--数据存储之File文件存储
转载来自:http://blog.csdn.net/ahuier/article/details/10364757,并进行扩充 引言:Android开发中的数据存储方式 Android提供了5种方式存 ...
- android开发中的5种存储数据方式
数据存储在开发中是使用最频繁的,根据不同的情况选择不同的存储数据方式对于提高开发效率很有帮助.下面笔者在主要介绍Android平台中实现数据存储的5种方式. 1.使用SharedPreferences ...
随机推荐
- java之String类在堆栈存储机制
String类是一个比较特殊的类,最主要的体现是它有多种创建形式,例如,String a ="abc";Sting a=new("abc");表面上看得到的结果 ...
- pandas基础运算
重新索引 (1)reindex重新索引,在已有的索引基础上新建索引,fill_value可以指定新建索引默认值 (2)#新建索引,如果新建的索引值为空自动填充之前的值 对于DataFrame重新索引同 ...
- java 大数据运算 BigInteger BigDecimal
package cn.sasa.demo5; import java.math.BigDecimal; import java.math.BigInteger; public class BigDat ...
- python pip install 报错TypeError: unsupported operand type(s) for -=: 'Retry' and 'int' Command "python setup.py egg_info" failed with error code 1 in
pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl ...
- IO和NIO
一.创建IO System.out.println( "*************欢迎进入文件操作系统*************" ); System.out.println( & ...
- tail命令 输出文件后n行,默认查看文件的后10行
默认查看文件的后10行 -n 3 数字 也可以忽略-n 直接加数字 tail 3 查看文件后3行 [root@localhost ~]# tail /etc/passwd // 默认查看文件的后十 ...
- 在Ubuntu上实现局域网共享文件夹
在Ubuntu上实现局域网共享文件夹如果你的系统是Ubuntu 14.04.14.10或12.04,有两个方法可以使你通过局域网在搭载Windows或其他Linux的电脑上共享本地文件.对局域网中的每 ...
- SQL Server 2016 发送邮件功能
--1 安装好SQL Server 2016 --2 安装.Net 3.5 由于SQL Server 2016 安装不提示强制安装.NET 3.5 但是还是需要安装,数据库发送邮件会使用.NET 3. ...
- 火币网API文档——WebSocket API Reference
订阅 KLine 数据 market.$symbol.kline.$period 成功建立和 WebSocket API 的连接之后,向 Server 发送如下格式的数据来订阅数据: { " ...
- dbdeployer安装TokuDB MySQL
下载最新的dbdeployer1.6.0,使用非root账户安装dbdeployer,特别是mv的时候. 1,解压 dbdeployer unpack Percona-Server-5.7.22-22 ...