Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created January 18, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/7570707cde8a4f4346520f0095328144 to your computer and use it in GitHub Desktop.
Save ZiTAL/7570707cde8a4f4346520f0095328144 to your computer and use it in GitHub Desktop.
python: erlojua interneteko web orri batekin zinkronizatzeko script-a
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pycurl
from io import BytesIO
import re
from subprocess import Popen, PIPE
url = 'https://www.timeanddate.com'
headers = BytesIO()
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.HEADER, 1)
c.setopt(c.NOBODY, 1) # header only, no body
c.setopt(c.HEADERFUNCTION, headers.write)
c.setopt(c.WRITEDATA, buffer)
c.perform()
headers = headers.getvalue()
headers = headers.decode("utf-8")
headers = re.split(r'\r\n', headers)
date = None
for i in headers:
if(re.search(r'^Date', i)):
date = i
break
if date:
date = re.sub(r'^Date:\s+', '', date)
Popen('/bin/date -s "'+date+'"', shell=True, stdout=PIPE).stdout.read()
print(date)
else:
print('date-sync: ERROR: NO TIME')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment