简体   繁体   English

如何使用Flask在Python中将数据从服务器发送到客户端?

[英]How to send data from server to client in Python using Flask?

I'm making an application that sends data to a client, then the client prints the data. 我正在制作一个将数据发送到客户端的应用程序,然后客户端打印数据。

I'm using Flask as a back-end framework that handles the server side, and another python script that generates random ID for the current client, the client checks every 4 seconds if new data comes in, if it gets data it should print it. 我使用Flask作为处理服务器端的后端框架,并使用另一个python脚本为当前客户端生成随机ID,客户端每4秒检查一次是否有新数据进入,如果有新数据则应打印该数据。

Back-end code 后端代码

@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})

Client script 客户端脚本

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()

Everything works, but if I send data from the server to the client it prints: 一切正常,但是如果我将数据从服务器发送到客户端,它将打印:

{'data': '', 'client_id': null}

In this piece of code: 在这段代码中:

@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})

You return JSON with client_id and data values, which you get from query parameters ( request.args ), but you don't send those parameters ( urllib2.urlopen(handler_url) ). 您将返回带有client_iddata值的JSON,这些值是从查询参数( request.args )获取的,但不发送这些参数( urllib2.urlopen(handler_url) )。

The client_id is not passed to the server, the server expects to get client_id and data as query parameters. client_id未传递到服务器,服务器希望将client_iddata作为查询参数。 Instead you are accessing the url without any parameters 相反,您访问的网址没有任何参数

file = urllib2.urlopen(handler_url) . file = urllib2.urlopen(handler_url)

Passing query string parameters to GET request can be done with 可以通过以下方式将查询字符串参数传递给GET请求:

url = handler_url + '?client_id' + self.client_id +'&data=YOURDATA_HERE'
file = urllib2.urlopen(url)

You can probably use urlencode to make this in a more elegant way. 您可能可以使用urlencode使其更优雅。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python flask从客户端向服务器发送和接收XML文件 - Send and recieve an XML file from client to server using python flask 如何使用 python flask web 服务从服务器发送文件以及如何从客户端获取这些文件? - how to send files using python flask web service from server and how to get those files from client? 如何使用 Python 将数据从客户端套接字发送到服务器套接字? - How to send data from client socket to server socket using Python? 如何使用python将数据从服务器发送到客户端? - how send data from server to client using python twisted? 如何将信息从客户端发送到服务器(Flask-python) - How to send information from client to server (Flask - python) 如何将数据从android应用程序(客户端)发送到python flask(后端)远程服务器 - How to send data from an android application (client) to a python flask (backend) remote server 如何使用python(服务器)和Angular(客户端)从SQL Server发送pdf数据并保存 - How to send pdf data and save from SQL Server using python(server) and Angular (client) 使用python将带有类别的数据从客户端发送到服务器 - send data with categories from client to a server using python Python / Flask / Ajax:从客户端向服务器发送字典密钥 - Python / Flask / Ajax : send a dict key from client to server 如何将数据从 python 客户端发送到 Django web 服务器? - How to send data from python client to Django web server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM