title: Python - Returning query result from server to client in Flask
body: <ul>
  <li><p><strong>What I have</strong></p>
  <p>I have a Client/Server in Flask. The client sends a query in JSON format to the server and the server creates a JSON file. There is another tool which takes this query, executes it on a db and writes the result to a results.txt file. The server periodically checks the 'results' directory for .txt files and if it finds a new file it extracts the result. For the periodic checking part I used APS.</p>
  </li>
  <li><p><strong>What I want to do</strong>
  Now I want to send this data (queryResult) which the server has extracted from the .txt file back to the client.</p>
  </li>
  </ul>
  <p>This is what I have done so far.</p>
  <ul>
  <li><strong>Server Code:</strong></li>
  </ul>
  <blockquote>
  <pre><code>app = Flask(__name__)
  api = Api(app)
  
  # Variable to store the result file count in the Tool directory
  fileCount = 0
  
  # Variable to store the query result generated by the Tool
  queryResult = 0    
      
  # Method to read .txt files generated by the Tool
  def readFile():
      global fileCount
      global queryResult
      # Path where .txt files are created by the Tool
      path = &quot;&lt;path&gt;&quot;
      tempFileCount = len(fnmatch.filter(os.listdir(path), '*.txt'))
      if (fileCount != tempFileCount):
          fileCount = tempFileCount
          list_of_files = glob.iglob(path + '*.txt')
          latest_file = max(list_of_files, key=os.path.getctime)
          print(&quot;\nLast modified file: &quot; + latest_file)
          with open(latest_file, &quot;r&quot;) as myfile:
              queryResult = myfile.readlines()
              print(queryResult) # I would like to return this queryResult to the client
              
  scheduler = BackgroundScheduler()
  scheduler.add_job(func=readFile, trigger=&quot;interval&quot;, seconds=10)
  scheduler.start()   
  
  # Shut down the scheduler when exiting the app
  atexit.register(lambda: scheduler.shutdown())   
     
  # Method to write url parameters in JSON to a file
  def write_file(response):
      time_stamp = str(time.strftime(&quot;%Y-%m-%d_%H-%M-%S&quot;))  
      with open('data' + time_stamp + '.json', 'w') as outfile:
          json.dump(response, outfile)
      print(&quot;JSON File created!&quot;)
     
  
  class GetParams(Resource):
      def get(self):
          response = json.loads(list(dict(request.args).keys())[0])  
          write_file(response)  
             
  api.add_resource(GetParams, '/data')  # Route for GetJSON()
  
  if __name__ == '__main__':
      app.run(port='5890', threaded=True)
  </code></pre>
  </blockquote>
  <ul>
  <li><strong>Client Code</strong></li>
  </ul>
  <blockquote>
  <pre><code>data = {
      'query': 'SELECT * FROM table_name'
  }
  
  url = 'http://127.0.0.1:5890/data'
  
  session = requests.Session()
  retry = Retry(connect=3, backoff_factor=0.5)  
  adapter = HTTPAdapter(max_retries=retry)
  session.mount('http://', adapter)
  session.mount('https://', adapter)
  
  resp = session.get(url, params=json.dumps(data))
  print(resp)
  </code></pre>
  </blockquote>
  <p>Can anyone please help me as to how to send this queryResult back to the Client?</p>
  <p>EDIT: I would like the server to send the queryResult back to the client each time it encounters a new file in the Tool directory i.e., every time it finds a new file, it extracts the result (which it is doing currently) and send it back to the client.</p>