# Python API - How to get data from the uplink\_callback method?

**URL:** <https://www.thethingsnetwork.org/forum/t/python-api-how-to-get-data-from-the-uplink-callback-method/27744>\
**Category:** Application Development\
**Created:** [July 3, 2019, 8:19pm UTC](https://www.thethingsnetwork.org/forum/t/python-api-how-to-get-data-from-the-uplink-callback-method/27744 "2019-07-03T20:19:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quers](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/quers/32/25844_2.png) [@Quers](https://www.thethingsnetwork.org/forum/u/Quers)\
**Post date:** [July 3, 2019, 8:19pm UTC](https://www.thethingsnetwork.org/forum/t/python-api-how-to-get-data-from-the-uplink-callback-method/27744/1 "2019-07-03T20:19:15Z")

</div>

Hi,

I’m currently trying to get a copy of the UplinkMessage object from the uplink\_callback method to process some values in some kind of endless loop with the Python API.  
My code looks something like this:

```auto
import time
import ttn
import csv
import os, sys
import queue

#Configuration data
app_id = "------------------------"
access_key = "--------------------"

message_path = os.path.abspath(os.path.dirname(sys.argv[0]))
message_path += '/messages.csv'

last_message = queue.Queue() #initialises a first in first out queue

#Define uplink method
def uplink_callback(msg, client):
    #Create CSV file with message content
    with open (message_path,'a',newline='') as message_file:
        csv_writer=csv.writer(message_file)
        csv_writer.writerow([msg.app_id,msg.dev_id,msg.payload_raw,msg.payload_fields.altitude,msg.payload_fields.hdop,msg.payload_fields.latitude,msg.payload_fields.longitude,msg.metadata.time])
        csv_writer.close()
    global last_message
    last_message.put((msg))

#Create handler for "The Things Network"
handler = ttn.HandlerClient(app_id, access_key, discovery_address="discovery.thethings.network:1900")

#Start mqtt client
mqtt_client = handler.data()
mqtt_client.set_uplink_callback(uplink_callback)
mqtt_client.connect()

while True:
    time.sleep(10)
    if not last_message.empty():
        print(last_message.get())

```

Despite the “last\_message” queue is flagged as global in the uplink\_callback method, I wasn’t able to show the data in the endless loop.  
Does anyone has an answer to this issue?

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [July 3, 2019, 9:46pm UTC](https://www.thethingsnetwork.org/forum/t/python-api-how-to-get-data-from-the-uplink-callback-method/27744/2 "2019-07-03T21:46:38Z")

</div>

Please [format your post](https://www.thethingsnetwork.org/forum/t/how-do-i-format-my-forum-post/8735) when including code to keep things readable. Use the pencil icon to edit your previous post to format it afterwards.
