Python Forum
Avoiding cached results via URLLIB - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Avoiding cached results via URLLIB (/thread-225.html)



Avoiding cached results via URLLIB - magpie5212 - Oct-01-2016

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)



  



RE: Avoiding cached results via URLLIB - wavic - Oct-01-2016

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


RE: Avoiding cached results via URLLIB - magpie5212 - Oct-01-2016

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'


RE: Avoiding cached results via URLLIB - wavic - Oct-01-2016

If that is the case change the user-agent. This might help. I think urllib sends Python as default user-agent.


RE: Avoiding cached results via URLLIB - magpie5212 - Oct-01-2016

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.


RE: Avoiding cached results via URLLIB - wavic - Oct-01-2016

Good! I don't know this site. If you like look at https://www.pythonanywhere.com/try-ipython/


RE: Avoiding cached results via URLLIB - snippsat - Oct-01-2016

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)



RE: Avoiding cached results via URLLIB - magpie5212 - Oct-02-2016

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.