Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import psycopg2
  2.  
  3. def generateDicts(log_fh):
  4. currentDict = {}
  5. a = []
  6. for line in log_fh:
  7. #currentDict = {"IP":line.split(" ")[0], "DATE":line.split(" ")[1]}
  8. #ip =["IP:"+line.split(" ")[0],"DATE:" + line.split(" ")[1]]
  9. ip = [line.split(" ")[0], line.split(" ")[3], line.split(" ")[8],line.split(" ")[9]]
  10. a.append(ip)
  11.  
  12. return a
  13.  
  14. count = 0
  15.  
  16. with open("/home/stiv/working/drug-abook.access.log") as f:
  17. for line in list(generateDicts(f)):
  18. ip = line[0]
  19. date = line[1]
  20. code = int(line[2])
  21. bytes = int(line[3])
  22. #print line
  23.  
  24.  
  25. conn = psycopg2.connect("dbname='logs_nginx' user='pit' host='localhost' password='123'")
  26. cur = conn.cursor()
  27. query = "INSERT INTO logs (ip, date, code, bytes) VALUES (%s, %s, %s, %s);"
  28. data = (ip, date, code, bytes)
  29. cur.execute(query, data)
  30. count=count +1
  31.  
  32. conn.commit()
  33.  
  34. print count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement