Revision e820685c lib/http.py
b/lib/http.py | ||
---|---|---|
150 | 150 |
code = 505 |
151 | 151 |
|
152 | 152 |
|
153 |
class ApacheLogfile: |
|
154 |
"""Utility class to write HTTP server log files. |
|
155 |
|
|
156 |
The written format is the "Common Log Format" as defined by Apache: |
|
157 |
http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#examples |
|
158 |
|
|
159 |
""" |
|
160 |
def __init__(self, fd): |
|
161 |
"""Constructor for ApacheLogfile class. |
|
162 |
|
|
163 |
Args: |
|
164 |
- fd: Open file object |
|
165 |
|
|
166 |
""" |
|
167 |
self._fd = fd |
|
168 |
|
|
169 |
def LogRequest(self, request, format, *args): |
|
170 |
self._fd.write("%s %s %s [%s] %s\n" % ( |
|
171 |
# Remote host address |
|
172 |
request.address_string(), |
|
173 |
|
|
174 |
# RFC1413 identity (identd) |
|
175 |
"-", |
|
176 |
|
|
177 |
# Remote user |
|
178 |
"-", |
|
179 |
|
|
180 |
# Request time |
|
181 |
self._FormatCurrentTime(), |
|
182 |
|
|
183 |
# Message |
|
184 |
format % args, |
|
185 |
)) |
|
186 |
self._fd.flush() |
|
187 |
|
|
188 |
def _FormatCurrentTime(self): |
|
189 |
"""Formats current time in Common Log Format. |
|
190 |
|
|
191 |
""" |
|
192 |
return self._FormatLogTime(time.time()) |
|
193 |
|
|
194 |
def _FormatLogTime(self, seconds): |
|
195 |
"""Formats time for Common Log Format. |
|
196 |
|
|
197 |
All timestamps are logged in the UTC timezone. |
|
198 |
|
|
199 |
Args: |
|
200 |
- seconds: Time in seconds since the epoch |
|
201 |
|
|
202 |
""" |
|
203 |
(_, month, _, _, _, _, _, _, _) = tm = time.gmtime(seconds) |
|
204 |
format = "%d/" + MONTHNAME[month] + "/%Y:%H:%M:%S +0000" |
|
205 |
return time.strftime(format, tm) |
|
206 |
|
|
207 |
|
|
208 | 153 |
class HTTPJsonConverter: |
209 | 154 |
CONTENT_TYPE = "application/json" |
210 | 155 |
|
Also available in: Unified diff