Python http.server doesn't print log
I am using Git Bash to start a simple local server with $ python3 -m http.server 8000
Git Bash never prints that it is running and does not receive the request log. I can access http: // localhost: 8000 / for the command to work. How do I get Git Bash to print the log?
source to share
This appears to be a common line buffering issue in mingw. You should be able to work around this by avoiding buffering the outputs, which in your case is possible via
$ python -u -m http.server 8080
This works as expected on
$ uname -a
MINGW64_NT-6.3 - 2.5.0(0.295/5/3) 2016-03-31 18:47 x86_64 Msys
source to share
From looking at the "what" of Git Bash, it looks like it's a shorthand version of cygwin. I just ran "python3 -m http.server 8000" and got the following log output:
$ python3 -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [04/Jun/2017 20:15:10] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [04/Jun/2017 20:15:10] code 404, message File not found
127.0.0.1 - - [04/Jun/2017 20:15:10] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [04/Jun/2017 20:15:10] code 404, message File not found
127.0.0.1 - - [04/Jun/2017 20:15:10] "GET /favicon.ico HTTP/1.1" 404 -
It would seem that Git bash is writing (outputting) wrong.
Try running "python3 -m http.server 8000" in virtualbox / vagrant -> with Ubuntu or Centos.
source to share
The method log_message
is called in the request handler. One thing you can do is override this method in your own handler and provide it with the output of whatever interests you. For example, when you run the script below, you will get a web server that prints the remote addresses and times of each request:
from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
class LoggingRequestHandler(SimpleHTTPRequestHandler):
def log_message(self, format, *args):
print(self.address_string(), self.log_date_time_string())
server = HTTPServer(('', 8080), LoggingRequestHandler)
server.serve_forever()
source to share