質問編集履歴
1
追記:現在のコード
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,3 +1,66 @@
|
|
1
1
|
> [参考HP](http://d.hatena.ne.jp/lolloo-htn/20090306)
|
2
2
|
こちらのソースコードを参考にapacheのlogをDBに保存しようとしたのですが、ログでbytesの部分が"-"になっている時に下記のエラーが出てきてしまいます。どうしたらいいですか?
|
3
|
-
> ValueError: invalid literal for int() with base 10: '-'
|
3
|
+
> ValueError: invalid literal for int() with base 10: '-'
|
4
|
+
|
5
|
+
```lang-<python>
|
6
|
+
#coding:utf8
|
7
|
+
|
8
|
+
from sqlobject import *
|
9
|
+
import sys,csv, sqlite3, datetime
|
10
|
+
|
11
|
+
logname = "short_access_log"
|
12
|
+
|
13
|
+
class ApacheDialect(csv.Dialect):
|
14
|
+
delimiter = " "
|
15
|
+
doublequote = True
|
16
|
+
lineterminator="\r\n"
|
17
|
+
quotechar='"'
|
18
|
+
quoting=0
|
19
|
+
skipinitialspace=False
|
20
|
+
|
21
|
+
class ApacheLog(SQLObject):
|
22
|
+
ip = StringCol()
|
23
|
+
remotelog = StringCol()
|
24
|
+
remoteuser= StringCol()
|
25
|
+
time = DateTimeCol()
|
26
|
+
request = StringCol()
|
27
|
+
status = IntCol()
|
28
|
+
bytes = IntCol()
|
29
|
+
referer = StringCol()
|
30
|
+
ua = StringCol()
|
31
|
+
|
32
|
+
def _set_time(self,timestr):
|
33
|
+
self._SO_set_time( datetime.datetime.strptime(timestr,"[%d/%b/%Y:%H:%M:%S") )
|
34
|
+
|
35
|
+
if __name__ == '__main__':
|
36
|
+
|
37
|
+
sqlhub.processConnection = connectionForURI('sqlite:///tmp/abc.db')
|
38
|
+
ApacheLog.createTable(ifNotExists=True)
|
39
|
+
|
40
|
+
csv.register_dialect("apache",ApacheDialect)
|
41
|
+
reader = csv.reader(open(logname),dialect="apache")
|
42
|
+
|
43
|
+
for r in reader:
|
44
|
+
ApacheLog(
|
45
|
+
ip = r[0],
|
46
|
+
remotelog = r[1],
|
47
|
+
remoteuser= r[2],
|
48
|
+
time = r[3],
|
49
|
+
#tz = r[4],
|
50
|
+
request = r[5],
|
51
|
+
status = int(r[6]),
|
52
|
+
bytes = int(r[7]),
|
53
|
+
referer = r[8],
|
54
|
+
ua = r[9]
|
55
|
+
)
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
この場合のエラーメッセージは
|
60
|
+
> Traceback (most recent call last):
|
61
|
+
File "log2db.py", line 46, in <module>
|
62
|
+
bytes = int(r[7]),
|
63
|
+
ValueError: invalid literal for int() with base 10: '-'
|
64
|
+
|
65
|
+
です。
|
66
|
+
[参考HP変更しました。](http://d.hatena.ne.jp/lolloo-htn/20090306/1236293162#c)
|