Skip to content

Instantly share code, notes, and snippets.

@ZiTAL ZiTAL/date-sync.py
Created Jan 18, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.