python - Save logs - SimpleHTTPServer -
how can save output console like
"192.168.1.1 - - [18/aug/2014 12:05:59] code 404, message file not found"
to file?
here code:
import simplehttpserver import socketserver port = 1548 handler = simplehttpserver.simplehttprequesthandler httpd = socketserver.tcpserver(("", port), handler) print "serving @ port", port httpd.serve_forever()
basehttprequesthandler.log_message()
prints log messages writing sys.stderr
. have 2 choices:
1) continue using basehttprequesthandler.log_message()
, change value of sys.stderr
:
import sys sys.stderr = open('logfile.txt', 'w') httpd.serve_forever()
2) create new xxxrequesthandler
class, replacing .log_message()
:
import simplehttpserver import socketserver import sys port = 1548 class myhttphandler(simplehttpserver.simplehttprequesthandler): log_file = open('logfile.txt', 'w') def log_message(self, format, *args): self.log_file.write("%s - - [%s] %s\n" % (self.client_address[0], self.log_date_time_string(), format%args)) handler = myhttphandler httpd = socketserver.tcpserver(("", port), handler) print "serving @ port", port httpd.serve_forever()
Comments
Post a Comment