Python Forum
Web App That Request Data from Another Web Site every 12-hours
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Web App That Request Data from Another Web Site every 12-hours
#5
(Sep-22-2018, 12:31 PM)jomonetta Wrote: does the server able to execute the other independent script which uses cron?
Can get some solution with cron to work,but as server is running there are several solution made for this APScheduler,Celery,schedule, ect...

Here example with APScheduler,it's nice as can add job for several functions.
It run with BackgroundScheduler so it will not interfef with main loop that server run.
from flask import Flask, render_template, jsonify
from apscheduler.schedulers.background import BackgroundScheduler 
import random
import requests

app = Flask(__name__)
def parse_func():
    response = requests.get('https://nghttp2.org/httpbin/get')
    r = response.json()
    lst = [r['url'], r['origin']]
    rand_value = random.choice(lst) 
    return(rand_value) 

@app.route("/")
def template_test():
    return render_template('sh2.html', location=parse_func())  

if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    scheduler.add_job(parse_func, 'interval', seconds=15)
    scheduler.start()
    app.run(debug=True)
So now calling parse_func() every 15-sec,longer eg hour=12.
Just to see values on client side use jinja,with a reload script.
sh2.html:
<!doctype html>
<html>
<head>
  <title>Some title</title>
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
</head>
<body>    
  <p> {{ location }}</p>
</body>
<script> 
    function timedRefresh(timeoutPeriod) {
      setTimeout("location.reload(true);",timeoutPeriod);
    }    
    window.onload = timedRefresh(15000);   
</script>
</html>
Can also look at this post.
Reply


Messages In This Thread
RE: Web App That Request Data from Another Web Site every 12-hours - by snippsat - Sep-23-2018, 05:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to scraping data from dinamic site sergio21124444 2 780 Nov-08-2023, 12:43 PM
Last Post: sergio21124444
  POST request with form data issue web scraping hoff1022 1 2,742 Aug-14-2020, 10:25 AM
Last Post: kashcode
  Scraping a dynamic data-table in python through AJAX request filozofo 1 3,936 Aug-14-2020, 10:13 AM
Last Post: kashcode
  How to retrieve data from site ROHK 2 2,491 Mar-01-2019, 12:26 PM
Last Post: ROHK
  Mechanize and BeautifulSoup read not correct hours vaeVictis 5 4,522 Jan-15-2019, 01:27 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020