I would like a python3 program to set the timezone according to the web ip. I am able to get the time zone location and change the time zone in python3 when I leave it returns to UTC.
Code: Select all
import requests
from urllib.request import urlopen
import re
import os
os.system('date')
#Mon 24 Apr 11:18:03 PDT 2017
#Mon 24 Apr 12:18:04 MDT 2017
#url = 'http://checkip.dyndns.org/'
def OutSideIP(url = 'http://checkip.dyndns.org/'):
request = urlopen(url).read().decode('utf-8')
MyIP = re.findall("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", request)
#MyIP = str(MyIP)
MyIP = str(MyIP[0])
return MyIP
def IPLocation(ip):
global js
url = 'http://freegeoip.net/json/'+ip
r = requests.get(url)
js = r.json()
try:
ip = OutSideIP('http://XXX.168.X.XXX')#my local modem address
pass
except OSError:
ip = OutSideIP()
pass
try:
IPLocation(ip)
pass
except:
pass
TimeZoneNew = js['time_zone']
os.system('export TZ=%s' % TimeZoneNew)
#os.environ['TZ'] = js['time_zone']
#os.environ.update()
js['country_code']
js['country_name']
js['time_zone']
os.system('date')
exit()
they all work while python3 is working. I would like to run this at the beginning and have it look up the location and change the TZ according.
I have tried under "sudo python3" also.