nginx+uwsgi+ExtJs4+web.py+sqlite3

目录

ubuntu安装uwsgi

sudo apt-get install python-dev
wget  http://projects.unbit.it/downloads/uwsgi-2.0.4.tar.gz
tar xvf uwsgi-2.0.4.tar.gz 
cd uwsgi-2.0.4
python setup.py build
make
sudo cp uwsgi /usr/bin	
#或者
sudo add-apt-repository ppa:stevecrozz/ppa  
sudo apt-get update  
sudo apt-get install uwsgi

配置nginx

server {
    listen 99;
    root /data/htdocs/web;
    location /py {
        uwsgi_pass 127.0.0.1:9001;
        include uwsgi_params;
    }
}

目录结构

web# pwd
/data/htdocs/web
web# ls
db  ext  index.html  js  py  restart.sh  sessions  templates

#index.html 调用js中的Extjs代码绘制页面
#全部使用Extjs请求后端的web.py

启动uwsgi

/usr/bin/uwsgi -s 127.0.0.1:9001 --wsgi-file ./py/index.py

部分web.py代码

import web
import json
import sqlite3
import hashlib


web.config.debug = False

#使用sqlite数据库
cx = sqlite3.connect("./db/db.db")

urls = (
        "/py/test/print", "tprint"
        ,"/py/hello", "hello"
        ,"/py/login", "login"
        ,"/py/logout", "logout"
        ,"/py/main", "main"
        ,"/py/tree/hostTree","hostTree"
        ,"/py/tree/addNode","addNode"
        ,"/py/tree/deleteNode","deleteNode"
       )

app = web.application(urls, globals())

#使用session
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'count': 0})



def isLogin():
    try:
        if(session.loggedin == 1):
            return True
    except:
        return False

#用session检查登录
def checkLogin():
     if(not isLogin()):
         web.seeother('/')



#登录代码
class login:
      def POST(self):
          i=web.input()
          cu = cx.cursor()
          cu.execute("select passwd from users where name='"+i.username+"';")
          psd=cu.fetchone()
          sid=web.cookies().get('webpy_session_id')
          if(psd != None and psd[0]==MD5(i.password)):
             session.loggedin = 1
             session.username = i.username
             return "{success:true,stat:'ok'}"
          else:
             return "{stat:'fals'}"

#退出代码
class logout:
      def GET(self):
           checkLogin()
           session.loggedin = 0
           web.seeother('/')


#登录后显示页面
class main:
      def GET(self):
          checkLogin()
          render=web.template.render("templates")
          return render.main()

#
application = app.wsgifunc()



目录