Python Forum
Avoiding cached results via URLLIB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Avoiding cached results via URLLIB
#1
I have some code getting the position of the ISS from http://api.open-notify.org/iss-now.json

Each time I run the code I get new values - exactly as expected.  But if I repeat the call (with a sleep of 15 seconds) it gets the same value each time.

def plotiss(isstoplot):
  url = 'http://api.open-notify.org/iss-now.json'
  request=urllib.request.urlopen(url)
  response=request.read()
  result=json.loads(response)
  location = result['iss_position']
  newlat = location['latitude']
  newlon = location['longitude']
  print('Latitude: ', newlat)
  print('Longitude: ', newlon)
  # isstoplot is a turtle 
  isstoplot.goto(newlon, newlat)

plotiss(iss)
time.sleep(15)
plotiss(iss)
time.sleep(15)
plotiss(iss)



  
Reply
#2
Try to run two separated scripts/processes in two different terminals and see what they will return. The servers are those which use intensively caching. For performance.
Print the headers if there is something related to caching.

Also you may call urllib.urlcleanup() before calling urllib.urlopen().
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Thanks for the suggestions Wavic

If I put the url in a browser and keep pressing Refresh it changes each time so I don't think it is a server cache.

Trying to use clean up gives AttributeError: '<invalid type>' object has no attribute 'urlcleanup'
Reply
#4
If that is the case change the user-agent. This might help. I think urllib sends Python as default user-agent.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
I should have said I was running on the Trinket web site.  It seems to be a quirk there.

I copied the code over to PyCharm and it works.
Reply
#6
Good! I don't know this site. If you like look at https://www.pythonanywhere.com/try-ipython/
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Quote: But if I repeat the call (with a sleep of 15 seconds) it gets the same value each time.
It's a bad way to repeat with sleep.
Python has build in Event scheduler or use schedule.
And indentation in Python is 4-space not 1.

It seems strange to me that this work without decode to utf-8.
request.read() #this return bytes in Python 3.
import urllib.request
import json, time
import schedule

def plotiss():
     url = 'http://api.open-notify.org/iss-now.json'
     request=urllib.request.urlopen(url)
     response=request.read().decode('utf-8')
     result=json.loads(response)
     location = result['iss_position']
     newlat = location['latitude']
     newlon = location['longitude']
     print('Latitude: ', newlat)
     print('Longitude: ', newlon)

schedule.every(.15).minutes.do(plotiss)
while True:
   schedule.run_pending()
   time.sleep(1)
But you should really use Requests
import requests
import schedule, time

def plotiss():
     url = 'http://api.open-notify.org/iss-now.json'
     req = requests.get(url)
     result = req.json()
     location = result['iss_position']
     newlat = location['latitude']
     newlon = location['longitude']
     print('Latitude: {}'.format(newlat))
     print('Longitude: {}'.format( newlon))

schedule.every(.15).minutes.do(plotiss)
while True:
   schedule.run_pending()
   time.sleep(1)
Reply
#8
Thanks for the information on event scheduling. As you might tell I am pretty new to Python. Not used to a language where the indentation is significant - at least not since I used assembler.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Urllib error wintenrod 3 3,630 Sep-14-2020, 06:18 AM
Last Post: wintenrod
  Connectivity issue with urllib ronen1m 7 4,756 Nov-22-2018, 01:03 PM
Last Post: ronen1m

Forum Jump:

User Panel Messages

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