如何使用 Flask 在 Python 中将数据从服务器发送到客户端?
How to send data from server to client in Python using Flask?
我正在制作一个向客户端发送数据的应用程序,然后客户端打印数据。
我使用 Flask 作为处理服务器端的后端框架,另一个 python 脚本为当前客户端生成随机 ID,客户端每 4 秒检查一次是否有新数据进来,如果它获取数据,它应该打印出来。
后端代码
@app.route('/data/api/interact/<string:client_id>', methods=['GET', 'POST'])
@login_required
def interact(client_id):
global data
form = Interact()
data = ''
if form.is_submitted():
get_data = form.ct.data
if get_data == 'hello':
data = 'Hi how are you?'
return redirect(url_for('data_handler', client_id=client_id, data=form.ct.data))
return render_template('interact.html', form=form, client_id=client_id)
@app.route('/data/api/interact/handler/', methods=['GET', 'POST'])
def data_handler():
client_id = request.args.get('client_id')
get_data = request.args.get('data')
return json.dumps({'client_id': client_id, 'data': get_data})
客户端脚本
handler_url = 'http://192.168.0.102:5000/data/api/interact/handler/'
class check_data(threading.Thread):
def __init__(self, client_id):
threading.Thread.__init__(self)
self.event = threading.Event()
self.client_id = client_id
def run(self):
global handler_url
try:
while not self.event.is_set():
file = urllib2.urlopen(handler_url)
xml = file.read()
print xml
file.close()
except:
pass
self.event.wait(4)
def new_client():
client_id = 'ClientId' + str(random.randrange(1, 500))
return client_id
client_id = 'null'
while client_id == 'null':
client_id = new_client()
if 'null' not in client_id:
break
print 'Client ID: ' + client_id
client = check_data(client_id)
client.start()
一切正常,但如果我将数据从服务器发送到客户端,它会打印:
{'data': '', 'client_id': null}
在这段代码中:
@app.route('/data/api/interact/handler', methods=['GET', 'POST'])
def data_handler():
client_id = request.args.get('client_id')
get_data = request.args.get('data')
return json.dumps({'client_id': client_id, 'data': get_data})
您 return JSON 具有 client_id
和 data
值,这些值是您从查询参数 (request.args
) 中获得的,但您不发送这些参数 (urllib2.urlopen(handler_url)
).
client_id
没有传递给服务器,服务器希望得到client_id
和data
作为查询参数。相反,您访问的 url 没有任何参数
file = urllib2.urlopen(handler_url)
.
Passing query string parameters 到 GET 请求可以用
完成
url = handler_url + '?client_id' + self.client_id +'&data=YOURDATA_HERE'
file = urllib2.urlopen(url)
您或许可以使用 urlencode
以更优雅的方式制作它。
我正在制作一个向客户端发送数据的应用程序,然后客户端打印数据。
我使用 Flask 作为处理服务器端的后端框架,另一个 python 脚本为当前客户端生成随机 ID,客户端每 4 秒检查一次是否有新数据进来,如果它获取数据,它应该打印出来。
后端代码
@app.route('/data/api/interact/<string:client_id>', methods=['GET', 'POST'])
@login_required
def interact(client_id):
global data
form = Interact()
data = ''
if form.is_submitted():
get_data = form.ct.data
if get_data == 'hello':
data = 'Hi how are you?'
return redirect(url_for('data_handler', client_id=client_id, data=form.ct.data))
return render_template('interact.html', form=form, client_id=client_id)
@app.route('/data/api/interact/handler/', methods=['GET', 'POST'])
def data_handler():
client_id = request.args.get('client_id')
get_data = request.args.get('data')
return json.dumps({'client_id': client_id, 'data': get_data})
客户端脚本
handler_url = 'http://192.168.0.102:5000/data/api/interact/handler/'
class check_data(threading.Thread):
def __init__(self, client_id):
threading.Thread.__init__(self)
self.event = threading.Event()
self.client_id = client_id
def run(self):
global handler_url
try:
while not self.event.is_set():
file = urllib2.urlopen(handler_url)
xml = file.read()
print xml
file.close()
except:
pass
self.event.wait(4)
def new_client():
client_id = 'ClientId' + str(random.randrange(1, 500))
return client_id
client_id = 'null'
while client_id == 'null':
client_id = new_client()
if 'null' not in client_id:
break
print 'Client ID: ' + client_id
client = check_data(client_id)
client.start()
一切正常,但如果我将数据从服务器发送到客户端,它会打印:
{'data': '', 'client_id': null}
在这段代码中:
@app.route('/data/api/interact/handler', methods=['GET', 'POST'])
def data_handler():
client_id = request.args.get('client_id')
get_data = request.args.get('data')
return json.dumps({'client_id': client_id, 'data': get_data})
您 return JSON 具有 client_id
和 data
值,这些值是您从查询参数 (request.args
) 中获得的,但您不发送这些参数 (urllib2.urlopen(handler_url)
).
client_id
没有传递给服务器,服务器希望得到client_id
和data
作为查询参数。相反,您访问的 url 没有任何参数
file = urllib2.urlopen(handler_url)
.
Passing query string parameters 到 GET 请求可以用
完成url = handler_url + '?client_id' + self.client_id +'&data=YOURDATA_HERE'
file = urllib2.urlopen(url)
您或许可以使用 urlencode
以更优雅的方式制作它。