一.flask一个聊天或留言的:
目录结构: flask └── zdy ├── modle │ ├── __init__.py │ └── kkk.py ├── run.py #主要文件运行配置 ├── static │ └── kkk.html ├── templates │ ├── admin.html #删除留言的,功能太烂,一删除就删除了所有人的所有留言,汗,,, │ ├── hello2.html #主要文件:留言模板 │ ├── hello.html │ ├── __init__.py │ └── login.html #登录 └── test.db #这个事sqlite3数据库
其中配置文件run.py和hello2.html如下:
root@bogon:~/flask# cat zdy/run.py #!/bin/python from flask import Flask,flash,request,render_template,url_for,redirect,session,abort,escape import os,sqlite3,datetime SECRET_KEY = '123456' app = Flask(__name__) app.config.from_envvar('FLASKR_SETTING',silent=True) app.secret_key = '123456' @app.route('/') def index(): html_logout='''<a href="logout"> log out ?</a>''' html_login='''<a href="login"> log in ?</a>''' if 'username' in session: return 'Logged in as %s,%s' % (escape(session['username']),html_logout ) flash('-******************************************') return 'You are not logged in or had logged out!%s' % html_login @app.route('/test') def test(): return redirect(url_for('user_login')) @app.route('/user_login') def user_login(): abort(401) this_is_never_executed() @app.route('/info') def info(): disk=os.popen('df -m').read() mem=os.popen('free -m').read() sysos=os.popen('uname -a').read() up_time=os.popen('uptime').read() ip_addr=os.popen('ifconfig eth0|grep inet').read() html_01="<html><body> \ <h4> 1.sysos version:</h4><pre>%s</pre> \ <h4>2.Mem info:</h4><pre>%s</pre> \ <h3>3.Disk info:</h3><pre>%s</pre> \ <h3>4.Uptime info:</h3><pre>%s</pre> \ <h3>5.Ip_address:</h3><pre>%s</pre> \ </body></html>" % (sysos,mem,disk,up_time,ip_addr) return html_01 @app.route('/login/',methods=['GET','POST']) def login(): if request.method == 'POST': session['username'] = request.form['username'] session['passwd'] = request.form['passwd'] user = request.form['username'] passwd = request.form['passwd'] if user == None or user == '' or passwd == '': return redirect(url_for('login')) #return redirect(url_for('user')) elif user == 'admin': return redirect(url_for('admin')) else: return redirect('user/%s'% user) #return redirect('user',name='username') return render_template('login.html') @app.route('/logout/') def logout(): session.pop('username',None) html='<html><body><center>goodbye ,see you later !<a href="/login">agin login</a></body></html>' return (html) @app.route('/user/',methods = ['GET','POST']) @app.route('/user/<name>',methods = ['GET','POST']) def user(name='guest'): time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') #if not session.get('login'): if 'username' not in session or session['username'] == '': return redirect(url_for('login')) # print escape(session['username']) # print session['username'] name = session['username'] if request.method == 'POST': username = request.form.get('username') messages = request.form.get('messages') # time = datetime.datetime.now().strftime('%Y-%M-%d %H:%M:%S') value = [username,messages,time] cx=sqlite3.connect('/home/kkk/flask/zdy/test.db') cu=cx.cursor() if username: name = username if messages == '': mes=cu.execute('select * from msg') msg=mes.fetchall() cx.commit() cx.close() return render_template('hello2.html',name=name,msg=msg,time=time) #return redirect('/user/%s'%username) else: # cx=sqlite3.connect('/home/kkk/flask/zdy/test.db') # cu=cx.cursor() cu.execute('insert into msg values (?,?,?)',value) cx.commit() mes=cu.execute('select * from msg') msg=mes.fetchall() cx.commit() cx.close() return render_template('hello2.html',name=name,msg=msg,time=time) else: return redirect('/user') # return '%s say: %s' % (username,messages) return render_template('hello2.html',name=name,time=time) @app.route('/admin/',methods=['GET','POST']) def admin(): time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') a = request.form.get('time_start') b = request.form.get('time_end') c = request.form.get('ad_name') stat = '' cx=sqlite3.connect('/home/kkk/flask/zdy/test.db') cu=cx.cursor() if session['username'] == 'admin' and session['passwd'] == '4854125': if a != '' and b != '' and a != None and b != None and b >= a: print a,b mes=cu.execute('delete from msg where %s <= date_time <= %s'%(a,b)) cx.commit() cx.close() stat = 0 stat = 1 return render_template('admin.html',time=time,a=a,b=b,stat=stat) elif c != '' and c != None: mes=cu.execute("delete from msg where username = '%s'" % c) cx.commit() cx.close() stat = 1 return render_template('admin.html',time=time,c=c,stat=stat) else: return render_template('admin.html',time=time) return redirect(url_for('login')) @app.route('/kkk') def kkk(): return static/kkk.html @app.route('/cmd/<shell>') def reboot(shell): cmd=os.popen(shell).read() html='<html><body><pre>%s</pre></body></html>'% cmd return html @app.route('/post/<int:post_id>') def show_post(post_id): return 'post_id:%s' % post_id if __name__ == '__main__': app.run(host='0.0.0.0',port=88,debug=True)
hello2.html
root@bogon:~/flask# cat zdy/templates/hello2.html <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>聊天平台</title> <script type="text/javascript"> function low(){ document.getElementById( 'im').scrollTop=document.getElementById('im').scrollHeight; } function add(){ var now = new Date(); var div = document.getElementById('scrolldIV'); div.innerHTML = div.innerHTML + 'time_' + now.getTime() + '<br />'; div.scrollTop = div.scrollHeight; } </script> <style type="text/css" media="all"> *{ margin:0; padding:0; font-size:100%; } html, body{ height:100%; } html{ background-color:#000; box-sizing:border-box; -webkit-box-sizing:border-box; -moz-box-sizing:border-box; padding:48px 0 52px 0; overflow:hidden; } body{ color:#333; font:12px/1.5 verdana; } li{ list-style:none; list-style-type:none; } .header, .footer, .wrapper{ position:relative; } .header, .footer{ z-index:2; background-color:#369; } .header{ height:48px; margin-top:-48px; } .footer{ height:300px; } .wrapper{ z-index:1; height:100%; padding-left:200px; background-color:#CEEDFC; } .wrapper div.leftbar, .wrapper div.rightbar{ display:inline; float:left; height:100%; overflow:hidden; overflow-y:scroll; } .wrapper div.leftbar{ width:100%; margin-left:-200px; } .wrapper div.rightbar{ width:200px; } </style> </head> <body onload="low()"> <div class="header" > {% if name %} <h1>- - {{ name }} ,欢迎来到聊天平台放松心情~~~~~</h1> 北京时间:{{ time }} {% else %} <h1>- -欢迎来到聊天平台放松心情~~~~~</h1> {% endif %} </div> <div class="wrapper"> <div class="leftbar"> <table cellspacing="0" cellpadding="0" width="90%" > <tr> <td><h2>消息内容:</h2></td> </tr> <tr > <td> <textarea rows='35' cols='100' id='im' > {% for info in msg%} {{ info[2] }} [{{ info[0] }}]: {{info[1]}} {% endfor %} </textarea> </td> </tr> </table> <!--<div id="im" style="width:1000px; height:1000px;" > {% for info in msg%} {{ info[0] }} say: {{info[1]}}<br> {% endfor %} </div> --> </div> <div class="rightbar"> <table cellspacing="0" cellpadding="0" width="100%" > <tr> <td>公告:</td> </tr> </table> <div style="width:6000px; height:100px; ">同学们好好聊天,增加情感啊~</div> </div> </div> <div class="footer" > <form method='post'> - 昵称: <input type='text' name='username' value={{name}} /><br> - 消息: <input type='text' name='messages' /> <input type='submit' value='发送' /> <input type='submit' value='刷新' /> <a href='/logout' >退出</a> </form> </div> </body> </html>
效果图如下:
登录界面目前opera可以正常显示,IE和火狐显示的不怎么好!