python轻型web框架flask的学习笔记 get post session cookie

flask 关于 get post session cookie的例子


from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
import subprocess, ipdb, re, ldap
from uggla import app
from uggla.users.models import User, UserManager, Group, GroupManager
@app.route('/users/')
def users_index():
    users = UserManager().all()
    return render_template('users/index.html', users=users)
@app.route('/users/add', methods=['GET', 'POST'])
def users_add():
    if request.method == 'POST':
        users = re.split('\r\n', request.form['list'])
        for uid in users:       
            user = User()
            user.create(uid)
            user.getUser(uid)
            flash('User %s added' % uid)
                                                                 
        try:
            user.saveEmailAliases(str(uid))
        except ldap.LDAPError, e:
            flash(e, category='error')
                                                                             
        users = UserManager().by_cn(users)
        return render_template('users/batch.html', users=users)
    return render_template('users/add.html')
@app.route('/users/edit/<uid>', methods=['GET', 'POST'])
def users_edit(uid):
    user = User()
                                                         
    if request.method == 'POST':
        user.getUser(uid)
        user.save()
        flash('User saved')
                                                             
        if(request.form['userPassword']):
            user.updatePassword(request.form['userPassword'])
            flash('Password updated')
        try:
            aliases = re.split('\r\n', str(request.form['emailAliases']))
            user.saveEmailAliases(filter(len, aliases))
        except ldap.LDAPError, e:
            flash(e, category='error')
                                                                     
        groups = request.form.getlist('groups')
        user.saveGroups(groups)
                                                             
        redirect('/users/edit %s' % uid)
                                                             
    user.getUser(uid)
    groups = GroupManager().all()
    return render_template('users/edit.html', user=user, groups=groups)
@app.route('/users/generate_password')
def generate_password():
    p = subprocess.Popen('apg -M NCL -m 12 -n 1'.split(), shell=False,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    return stdout
                                                         
@app.route('/users/batch/', methods=['POST'])
def users_batch():
    batch = request.form.getlist('batch')
    users = UserManager().by_cn(batch)
    groups = GroupManager().all()
                                                         
    return render_template('users/batch.html', users=users, groups=groups)
@app.route('/groups/')
def groups_index():
    groups = GroupManager().all()
    return render_template('groups/index.html', groups=groups)
@app.route('/groups/add', methods=['GET', 'POST'])
def groups_add():
    if request.method == 'POST':
        groups = re.split('\r\n', request.form['list'])
        for cn in groups:       
            group = Group()
            group.create(cn)
            group.getGroup(cn)
            flash('Group %s added' % cn)
                                                         
    return render_template('groups/add.html')
@app.route('/groups/edit/<cn>', methods=['GET', 'POST'])
def groups_edit(cn):
    group = Group()   
    group.getGroup(cn)
    return render_template('groups/edit.html', group=group)


如果遇到这个问题,很多时候是因为压力大出现的,解决的办法是用nginx的uwsgi 和gevent来解决。


Exception happened during processing of request from ('10.58.107.76', 53028)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
相关文章
相关标签/搜索