def get_lista_reporte(): carnet = request.args.get('carnet') res = {} res['atendido'] = str(os.getenv('SERVNAME')) db = Database() res['data'] = db.get_lista_reporte(carnet) return json.dumps(res, default=str)
def get_reporte(): reporte = request.args.get('reporte') res = {} res['atendido'] = str(os.getenv('SERVNAME')) db = Database() res['data'] = db.get_reporte(reporte) return json.dumps(res, default=str)
def register_page(): try: form = RegistrationForm(request.form) if request.method == "POST" and form.validate(): username = form.username.data email = form.email.data password = sha256_crypt.encrypt(str(form.password.data)) db = Database() if db.is_user_exists(username): flash( "This username is already taken. Please choose another username." ) return render_template('register.html', form=form) else: if db.insert_user(username, password, email): flash("Thanks for registration.") session['logged_in'] = True session['username'] = username return redirect(url_for('dashboard')) return render_template('register.html', form=form) except Exception as e: return str(e)
def add_reporte(): # {"carnet":0, "nombre":"nombre", "curso":"curso"} content = request.get_json() content['procesado'] = str(os.getenv('SERVNAME')) db = Database() res = db.add_reporte(content) res['atendido'] = str(os.getenv('SERVNAME')) return json.dumps(res)
解决 """ from socket import * from multiprocessing import Process import signal from time import sleep from mysql import Database import sys # 全局变量 # 后面会经常用到的变量将之提取到前面作为全局变量 HOST = '0.0.0.0' PORT = 8000 ADDR = (HOST, PORT) # 建立数据库对象 db = Database(database='dict') # 服务端注册处理 def do_register(c, data): tmp = data.split(' ') name = tmp[1] passwd = tmp[2] # 返回True表示注册成功,False表示失败 if db.register(name, passwd): c.send(b'OK') else: c.send(b'Fail') # 服务端登录
from flask import Flask, request from flask_cors import CORS from mysql import Database import simplejson as json app = Flask(__name__) cors = CORS(app, resources={r"/*": {"origin": "*"}}) db = Database() @app.route('/', methods=['GET']) def check(): return 'hola perros' @app.route('/ListarIdEvento', methods=['GET']) def ListarIdEvento(): content = request.get_json() #print(content) response = db.listEvent(content['idEvento']) if response == None: return "Record not found", 400 return json.dumps(response, 200) @app.route('/AgregarUsuario', methods=['POST']) def AgregarUsuario(): content = request.get_json() print(content)
def __init__(self): self.user = Database(database='test') self.query = Database(database='dict') self.__ss = self.__init_socket()
class DictServer: def __init__(self): self.user = Database(database='test') self.query = Database(database='dict') self.__ss = self.__init_socket() def __init_socket(self): ss = socket() ss.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) ss.bind(('0.0.0.0', 8888)) ss.listen(128) return ss def do_query(self, r, data): self.query.create_cursor() name = data[1] word = data[2] describes = self.query.do_query(word) if not describes: msg = 'not the explain' else: msg = '%s : %s' % (word, describes) r.send(msg.encode()) self.user.insert(name, word) def __handle(self, r): self.user.create_cursor() while True: data = r.recv(1024).decode().split() if not data or data[0] == 'QUIT': r.close() self.query.close() self.user.close() return elif data[0] == 'SIGNIN': self.user.sign_in(r, data[1], data[2]) elif data[0] == 'SIGNUP': self.user.sign_up(r, data[1], data[2]) elif data[0] == 'Q': self.do_query(r, data) elif data[0] == 'V': self.user.log(r, data[1]) def main(self): signal.signal(signal.SIGCHLD, signal.SIG_IGN) while True: try: cs, addr = self.__ss.accept() print('connect from', addr) except KeyboardInterrupt: self.__ss.close() sys.exit('退出服务器') except Exception as e: print("ERROR:", e) continue p = Process(target=self.__handle, args=(cs, )) p.daemon = True p.start()
r = self.cursor.fetchone() if r: return False try: sql = "INSERT INTO user (email, password) VALUES (%s, %s)" self.cursor.execute(sql, (username, password)) self.db.commit() r.send('注册成功!'.encode()) return True except Exception: self.db.rollback() r.send('系统繁忙'.encode()) return False db = Database(database='user') # 函数式服务器 def do_register(c, data): temp = data.split() name = temp[1] passwd = temp[2] if db.sign_up(c, name, passwd): c.send(b'ok') else: c.send(b'Fail') def request(c): db.create_cursor()
模型: 多进程tcp并发 """ from socket import * from multiprocessing import Process import signal, sys from mysql import Database from time import sleep # 全局变量 HOST = "0.0.0.0" PORT = 8000 ADDR = (HOST, PORT) # 建立数据库对象 db = Database(database="dict") # 服务端注册处理 def do_register(c, data): tmp = data.split(" ") name = tmp[1] passwd = tmp[2] # 返回True表示注册成功,False表示失败 if db.register(name, passwd): c.send(b"OK") else: c.send(b"Fail") # 登录