2017-04-03 63 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

相关问题