use requests library
This commit is contained in:
parent
664a2be2dd
commit
2889c32576
33
slog.py
33
slog.py
@ -1,8 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import urllib.request, datetime, os, time
|
import requests, datetime, os
|
||||||
from urllib.error import URLError, HTTPError
|
import sqlite3, time
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
day = now.strftime('%F')
|
day = now.strftime('%F')
|
||||||
@ -19,35 +18,17 @@ else:
|
|||||||
db = sqlite3.connect('stats.db')
|
db = sqlite3.connect('stats.db')
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
|
|
||||||
request = urllib.request.Request(url)
|
r = requests.get(url)
|
||||||
|
|
||||||
try:
|
if not r.status_code == 200:
|
||||||
response = urllib.request.urlopen(request)
|
error = r.status_code
|
||||||
|
|
||||||
except HTTPError as e:
|
|
||||||
# print('The server couldn\'t fulfill the request.')
|
|
||||||
# print('Error code: ', e.code)
|
|
||||||
error = e.code
|
|
||||||
data = None
|
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:
|
else:
|
||||||
# everything is fine
|
|
||||||
data = response.read().decode('utf-8')
|
|
||||||
error = None
|
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))
|
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, the_time, error, data))
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user