2.[文件]
communicate_two.py ~ 2KB
下载(14)
# -*- coding:utf8 -*-
#encoding = utf-8
import time
import MySQLdb
import time
from flask import Flask,g,request,make_response
import hashlib
import xml.etree.ElementTree as ET
app = Flask(__name__)
app.debug = True
from sae.const import (MYSQL_HOST, MYSQL_HOST_S, MYSQL_PORT, MYSQL_USER, MYSQL_PASS, MYSQL_DB)
#装饰器,实现数据库连接
@app.before_request
def before_request():
g.db = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, port = int(MYSQL_PORT), charset = 'utf8')
@app.teardown_request
def teardown_request(exception):
if hasattr(g, 'db'): g.db.close()
#用来响应comunicate_one给它发起请求
@app.route('/xxx2',methods=['GET','POST'])
def wechat_hardware():
if request.method == 'POST':
h_rec = request.stream.read()
h_rec_xml = ET.fromstring(h_rec)
to = h_rec_xml.find('ToUserName').text
fro = h_rec_xml.find('FromUserName').text
string_code = h_rec_xml.find('Content').text
c = g.db.cursor()
c.execute("select * from goods where bar_code = %s", string_code)
good_item = list(c.fetchall())
for row in good_item:
insert_name = unicode(row[0])
insert_price = unicode(row[1])
insert_code = unicode(row[2])
insert_item = [insert_name,insert_price,insert_code]
c.execute("insert ignore into my_fridge values(%s,%s,%s,now())",insert_item)
return make_response('i have get {0} from you '.format(string_code)+ 'insert success') #直接字符串响应
else:
return make_response('you get me successfully')