2017-04-03 38 views
0

我使用HTTPHandler到日誌記錄消息發送到與下面的代碼一個Django Web服務器,使用HttpHandler的原因Django的服務器側示出了HTTP 400和無效HTTP_HOST標頭消息

import logging 
import logging.handlers 

logger = logging.getLogger(__name__) 

_TARGET = '192.168.8.100:8000' 
_PATH = '/VideoParser/lYYDownloaderClientLog/' 

sh = logging.handlers.HTTPHandler(_TARGET, _PATH) 

logger.addHandler(sh) 
logger.error('testing remote logging') 

但服務器側示出了HTTP 400和如此無效的HTTP_HOST標頭消息

無效的HTTP_HOST標頭:'192.168.8.100:8000,192.168.8.100'。根據RFC 1034/1035,提供的 域名無效。 [05 /月/ 2017年10點43分十四秒]「GET /VideoParser/lYYDownloaderClientLog /?relativeCreated = 117.006540​​29846191 &螺紋= 5468 & levelname = ERROR & exc_info =無& exc_text =無& PROC ESS = 8920 &文件名= a.py &毫秒= 39.52503204345703 & stack_info =無& levelno = 40個& PROCES SNAME = MainProcess &味精=試驗+ +遠程登錄&模塊=一個& threadName = MainThread &線 沒有= 26 &創建= 1491360192.039525 &了funcName =%3Cmodule%3E & ARGS =%28%29 &名稱= & 路徑名= C%3A%5CUsers%5 Cl%5CDocuments%5CTencent +文件%5C2281570025%5CFileRecv%5C 一個。 PY HTTP/1.1" 400 68137

而來自瀏覽器的請求使用URL

http://192.168.8.100:8000/VideoParser/lYYDownloaderClientLog/?filename=log55.p%20y&levelno=40&relativeCreated=88.00482749938965&funcName=%3Cmodule%3E&thread=7144%20&stack_info=None&module=log55&args=%28%29&exc_text=None&pathname=D%3A%5CBaiduYun%20Download%5C%E7%BC%96%E7%A8%8B%5CPython%5Clog55.py&levelname=ERROR&msecs=668.0548%20191070557&threadName=MainThread&process=6664&name=root&lineno=34&msg=yahoo+Serve%20r+Exception&exc_info=None&processName=MainProcess&created=1491225161.6680548

實際上可以發送一個很好的請求到服務器,服務器顯示了在這種情況下,以下

----666666666666--- <QueryDict: {'stack_info': ['None'], 'levelno': ['40'], 'arg 
s': ['()'], 'exc_info': ['None'], 'created': ['1491225161.6680548'], 'process': 
['6664'], 'levelname': ['ERROR'], 'exc_text': ['None'], 'module': ['log55'], 'ms 
ecs': ['668.0548 191070557'], 'name': ['root'], 'processName': ['MainProcess'], 
'lineno': ['34'], 'thread': ['7144 '], 'msg': ['yahoo Serve r Exception'], 'func 
Name': ['<module>'], 'threadName': ['MainThread'], 'filename': ['log55.p y'], 'p 
athname': ['D:\\BaiduYun Download\\編程\\Python\\log55.py'], 'relativeCreated': 
['88.00482749938965']}> 
[05/Apr/2017 10:45:26] "GET /VideoParser/lYYDownloaderClientLog/?filename=log55. 
p%20y&levelno=40&relativeCreated=88.00482749938965&funcName=%3Cmodule%3E&thread= 
7144%20&stack_info=None&module=log55&args=%28%29&exc_text=None&pathname=D%3A%5CB 
aiduYun%20Download%5C%E7%BC%96%E7%A8%8B%5CPython%5Clog55.py&levelname=ERROR&msec 
s=668.0548%20191070557&threadName=MainThread&process=6664&name=root&lineno=34&ms 
g=yahoo+Serve%20r+Exception&exc_info=None&processName=MainProcess&created=149122 
5161.6680548 HTTP/1.1" 200 27 

有啥錯誤使用HTTPHandler到日誌信息發送到Django的Web服務器我的代碼?

正如我測試過的:如果我將Web服務器的IP地址傳遞給HTTPHandler的主機參數,服務器端將顯示http 400和Invalid HTTP_HOST標頭消息,也有例外,這裏粘貼https://bpaste.net/show/f2d2e64e7a7e,如果我傳遞域名,然後視圖函數按預期工作。 那麼它會成爲HTTPHandler中的一個錯誤?

相關的代碼進行調試

Django的測試\ LYYDownloaderServer \ VideoParser \ urls.py

from django.conf.urls import url 
from . import views 
app_name = 'VideoParser' 
urlpatterns = [ 
    url(r'lYYDownloaderClientLog.+',views.lYYDownloaderClientLog, name='lYYDownloaderClientLog') 
] 

Django的測試\ LYYDownloaderServer \ VideoParser \ views.py

def lYYDownloaderClientLog(request): 
    print('----666666666666---', request.GET) 
    return HttpResponse("") # The server *successfully* processed the request and is not returning any content. 
+0

貌似域名是錯誤的。你輸入正確嗎? –

+0

@LeonardoChirivì對不起,我這次更新了相應的服務器消息。 – iMath

+0

是192.168.8.100加入allowed_host? –

回答

0

我見面當我使用Django接收日誌時,同樣的問題,最後我沒有解決它,但我知道問題是。

Invalid HTTP_HOST header: '127.0.0.1:8888,127.0.0.1'. The domain name provided is not valid according to RFC 1034/1035. 

根據輸出,我檢查logging.handlers.HTTPHandler的源代碼,和在這個類中,功能emit將實例http.client.HTTPSConnectionhttp.client.HTTPConnection然後執行putrequestputheader,但在putrequest將執行putheader到,因爲該端口!= 80,主機是host:port

   if port == self.default_port: 
        self.putheader('Host', host_enc) 
       else: 
        host_enc = host_enc.decode("ascii") 
        self.putheader('Host', "%s:%s" % (host_enc, port)) 

,所以我想,如果你使用的Nginx和80端口,說不定可以解決問題

更新: 最後我解決它。我提出一個新的類LogHTTPHandler,繼承的HttpHandler,然後重寫功能emit和註釋掉h.putheader("Host", host)

from logging.handlers import HTTPHandler 

class LogHTTPHandler(HTTPHandler): 
    def emit(self, record): 
     ...... 
     i = host.find(":") 
     if i >= 0: 
      host = host[:i] 
     # h.putheader("Host", host) 
     if self.method == "POST": 
      h.putheader("Content-type", 
         "application/x-www-form-urlencoded") 
      h.putheader("Content-length", str(len(data))) 
     ...... 
+0

大調查!我曾經在我的本地機器上測試該程序,因此引發了錯誤消息,但是當我在運行nginx/1.4.4的遠程服務器上測試它時,根本沒有錯誤消息,所以也許您有正確的觀點。如果這是Python中的錯誤,請報告,謝謝! – iMath

+0

某人已經提交了錯誤https://bugs.python.org/issue30904 – iMath

相關問題