nginx uwsgi bottle postgresql psycopg2

时间:2017-02-21 22:58:13

标签: python nginx uwsgi psycopg2 bottle

我的要求:

大约300个(类似现场传感器)客户端每X(例如10)分钟报告一次状态(json字符串)。他们只能使用“curl”来进行报告。服务器上的API /处理程序需要解析并将所有这些值转储到主表中。  一些用户(通常少于10个)从他们的浏览器连接到应用程序以检查传感器报告的状态,并可能在检查几页(主状态页面,详细报告页面等)时徘徊。用户希望看到的页面少于10页(想想报告)。

我的设置:

Web服务器:nginx  App Server:uwsgi   框架:瓶子    数据库:PostgreSQL  Python DB驱动程序:psycopg2    前端:Bootstrap

我的代码:

请注意,我没有在代码中包含大量错误检查和其他安全措施,只是因为它们没有为此讨论做出贡献。

import os
from bottle import route, post, run, request, template, install, static_file
import psycopg2
import customemailservice

@route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='/webapp/ss/static')

# The URL that the sensors will hit to pass on their status report
@post('/logger')
def dolog():
    sensor_id = request.json['id']
    sensor_ts  = request.json['ts']
    sensor_ut = request.json['main']['ut']
    sensor_ploss = request.json['main']['ploss']
    sensor_s1 = request.json['main']['s1']
    sensor_s2 = request.json['main']['s2']
    sensor2_status = request.json['aux']['status']
    sensor2_rts = request.json['aux']['rts']
    try:
        conn = psycopg2.connect('dbname=<dbnane> user=<username> password=    <password> host=<dbhost> port=<dbport>')
    except psycopg2.Error as pe: 
    print pe.pgerror
    curr = conn.cursor()

    if conn != None and curr != None:
        curr.execute('''INSERT INTO tbllog (id, ts, ut, ploss, s1, s2, status, rts) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)''',(sensor_id, sensor_ts, sensor_ut, sensor_ploss, sensor_s1, sensor_s2, sensor2_status, sensor2_rts))
        conn.commit()
        curr.close()
        conn.close()
    else:
        pass
        # The code here is irrelevant
    return template('Ok from {{sid}} at {{ts}}', sid=sid,ts=ts)

@route('/')
def index():
    try:
        conn = psycopg2.connect('dbname=<dbnane> user=<username> password=<password> host=<dbhost> port=<dbport>')
    except psycopg2.Error as pe: 
        conn = None
        print pe.pgerror
    curr = conn.cursor()

    if conn != None and curr != None:
        sql = 'select t1.* from tbllog t1 where t1.ts = (select max(t2.ts) from tbllog t2 where t2.id=t1.id) order by id;'
        curr.execute(sql)
        rs=curr.fetchall()    
        html=""
        for row in rs:
            html = html + '<tr><td class="warning">' + row[0] + '</td><td class="warning">' + str(row[1]) + '</td><td class="success">' + str(row[2]) + '</td><td class="success">' + str(row[3]) + '</td><td class="success">' + str(row[4]) + '</td><td class="info">' + str(row[5]) + '</td><td class="info">' + str(row[6]) + '</td></tr>'
        curr.close()
        conn.close()
    # Pass the raw html table that will be inserted into the index template.
    return template('index',tdata=html)

@route('/status/<sensor_id>')
def getsensorid(sensor_id):
    try:
        conn = psycopg2.connect('dbname=<dbnane> user=<username> password=<password> host=<dbhost> port=<dbport>')
    except psycopg2.Error as pe: 
        conn = None
        print pe.pgerror
    curr = conn.cursor()

    if conn != None and curr != None:
        sql = 'select * from tbllog where id=\'' + sensor_id + '\' order by ts;'
        curr.execute(sql)
        rs=curr.fetchall()
        html=""
        for row in rs:
            html = html + '<tr class="info"><td>' + row[0] + '</td><td>' + str(row[1]) + '</td><td>' + str(row[2]) + '</td><td>' + str(row[3]) + '</td><td>' + str(row[4]) + '</td><td>' + str(row[5]) + '</td><td>' + str(row[6]) + '</td></tr>'
        curr.close()
        conn.close()

if __name__ == '__main__':
    run (host="0.0.0.0", port=8080, debug=True)
else:
    app = application = bottle.default_app()

我的问题:

鉴于要求,这是一种合理的方法吗?或者,您是否建议使用数据库连接池?我对使用池有点困惑,因为我不确定我的应用程序代码在什么级别(nginx,uwsgi或者瓶子)被复制(为并发客户端服务)以及我应该如何创建可以在不同的地方使用的池线程/进程(每个包含此应用程序代码的副本)。

显然,这是我最初涉足网络应用程序(甚至是严肃的Python)并且如果您认为有更好的方法(我假设有很多方法)可以让您回复猫。

0 个答案:

没有答案