2

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.

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}

2 Answers 2

3

The client_id is not passed to the server, the server expects to get client_id and data as query parameters. Instead you are accessing the url without any parameters

file = urllib2.urlopen(handler_url).

Passing query string parameters to GET request can be done with

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your interest , your answer also helped me ;)
2

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

4 Comments

do i need to provide the parameters to handler_url , or the client will do the job ??
i didn't noticed the request.args
Maybe it will be more effective if i used another route that will store these parameters temporarily in session and the client will send a request to this route where another return json will provide him the data , does that make sense !!!
thanks man it helps me a lot , know i am knowing from where to start fixing the code , salute :) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.