2017-06-04 410 views
2

我使用Git的Bash運行一個簡單的本地服務器$ python3 -m http.server 8000Python的http.server不打印日誌

的Git的Bash從來沒有打印出它正在運行,並沒有得到一個日誌要求。我可以訪問http://localhost:8000/,以便命令正常工作。如何讓git bash打印日誌?

+0

別人有同樣的問題可望解決此通過避免緩衝輸出,而你的情況是可能的:HTTPS:// stackoverflow.com/questions/35657355/python-logging-to-stdout-in-git-bash – fuglede

回答

2

這似乎是在mingw下線緩衝的一個普遍問題。您應該能夠通過

$ python -u -m http.server 8080 

這個工程上

$ uname -a 
MINGW64_NT-6.3 - 2.5.0(0.295/5/3) 2016-03-31 18:47 x86_64 Msys 
+0

這似乎是工作!謝謝 –

-1

在處理程序中根據請求調用方法log_message。你可以做的一件事是在你自己的處理程序中重寫這個方法,並讓它輸出你感興趣的任何東西。例如,運行下面的腳本,你將得到一個web服務器,它打印每個請求的遠程地址和時間:

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() 
0

從看「什麼」Git Bash,它似乎是一個cygwin的減少版本。我剛纔跑「python3 -m http.server 8000」,並獲得以下日誌輸出:

$ 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 - 

這似乎是git的慶典沒有記錄(輸出)正確。

嘗試在virtualbox/vagrant中使用Ubuntu或Centos運行「python3 -m http.server 8000」。