use requests library

This commit is contained in:
rscmbbng 2018-12-03 04:47:52 +01:00
parent 664a2be2dd
commit 2889c32576

33
slog.py
View File

@ -1,8 +1,7 @@
#!/usr/bin/env python3
import urllib.request, datetime, os, time
from urllib.error import URLError, HTTPError
import sqlite3
import requests, datetime, os
import sqlite3, time
now = datetime.datetime.now()
day = now.strftime('%F')
@ -19,35 +18,17 @@ else:
db = sqlite3.connect('stats.db')
c = db.cursor()
request = urllib.request.Request(url)
r = requests.get(url)
try:
response = urllib.request.urlopen(request)
except HTTPError as e:
# print('The server couldn\'t fulfill the request.')
# print('Error code: ', e.code)
error = e.code
if not r.status_code == 200:
error = r.status_code
data = None
pass
except URLError as e:
# print('We failed to reach a server.')
# print('Reason: ', e.reason)
error = e.reason
data = None
pass
else:
# everything is fine
data = response.read().decode('utf-8')
error = None
data = r.text
if error:
error = error.args[1]
print(day,the_time,error, data)
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, the_time, error, data))
db.commit()
db.close()