using HTTPHandler cause Django server side shows http 400 and Invalid HTTP_HOST header message

99 views
Skip to first unread message

Philip Lee

unread,
Apr 10, 2017, 12:21:40 PM4/10/17
to Django users

I also posted The question here 
http://stackoverflow.com/questions/43185804/using-httphandler-cause-django-server-side-shows-http-400-and-invalid-http-host 


I am using HTTPHandler to send logging messages to a Django Web server with The following code,

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')

but The server side shows http 400 and Invalid HTTP_HOST header message like this

Invalid HTTP_HOST header: '192.168.8.100:8000,192.168.8.100'. The domain name pr ovided is not valid according to RFC 1034/1035. [05/Apr/2017 10:43:14] "GET /VideoParser/lYYDownloaderClientLog/?relativeCreated =117.00654029846191&thread=5468&levelname=ERROR&exc_info=None&exc_text=None&proc ess=8920&filename=a.py&msecs=39.52503204345703&stack_info=None&levelno=40&proces sName=MainProcess&msg=testing+remote+logging&module=a&threadName=MainThread&line no=26&created=1491360192.039525&funcName=%3Cmodule%3E&args=%28%29&name=main& pathname=C%3A%5CUsers%5Ci%5CDocuments%5CTencent+Files%5C2281570025%5CFileRecv%5C a.py HTTP/1.1" 400 68137

while request from browser with 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

could actually send a good request to The server , The server shows The following in this case

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

so what's wrong with my code using HTTPHandler to send logging messages to a Django Web server ?

As I have tested: If I pass the IP address of my web server to the host parameter of HTTPHandler, the server side would show http 400 and Invalid HTTP_HOST header message, there are also exceptions , pasted here https://bpaste.net/show/f2d2e64e7a7e , while if I pass the domain name instead, then the view function works as expected . so would it be a bug within HTTPHandler?

related code For debug

django-test\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-test\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.

Camilo Torres

unread,
Apr 10, 2017, 2:25:58 PM4/10/17
to Django users
Hi,
Try with this configuration instead:
_TARGET = 'http://192.168.8.100:8000'

Notice you missed http:// there.

Zhao Lee

unread,
Apr 10, 2017, 10:24:49 PM4/10/17
to django...@googlegroups.com
No, that would cause the following exception 

--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 798, in _get_hostport
    port = int(host[i+1:])
ValueError: invalid literal for int() with base 10: '//ezvideo.0letter.com'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\logging\handlers.py", line 1137, in emit
    h = http.client.HTTPConnection(host)
  File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 762, in __init__
    (self.host, self.port) = self._get_hostport(host, port)
  File "C:\Users\i\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 803, in _get_hostport
    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
http.client.InvalidURL: nonnumeric port: '//ezvideo.0letter.com'
Call stack:
  File "C:\Users\i\Documents\Tencent Files\2281570025\FileRecv\a.py", line 26, in <module>
    logger.error('testing remote logging')
Message: 'testing remote logging'
Arguments: ()



--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/m42PLb1N_yo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsub...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8a6fcb79-dbf6-44f1-9971-7da02af85f27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


 

Reply all
Reply to author
Forward
0 new messages