Browse Source

use requests library

master
rscmbbng 6 years ago
parent
commit
2889c32576
  1. 33
      slog.py

33
slog.py

@ -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
data = None
pass
except URLError as e:
# print('We failed to reach a server.')
# print('Reason: ', e.reason)
error = e.reason
if not r.status_code == 200:
error = r.status_code
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()

Loading…
Cancel
Save