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)
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().
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'
If that is the case change the user-agent. This might help. I think urllib sends Python as default user-agent.
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.
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)
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.