[issue18229] attribute headers of http.server.BaseHTTPRequestHandler sometimes does not exists

Jordan Szubert report at bugs.python.org
Sun Oct 6 17:39:49 CEST 2013


Jordan Szubert added the comment:

#minimal server:
#!/c/Python33/python.exe
from http.server import HTTPServer as S, BaseHTTPRequestHandler as H
class HNDL(H):
	def log_request(req,code):
		print('header is',req.headers.get('X-Forwarder-For'),', code',code)
		H.log_request(req)
s=S(('',54321),HNDL)
s.serve_forever()


#non-http client:
#!/c/Python33/python.exe
import socket,os
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 54321))
s.sendall(os.urandom(1024))
buf=s.recv(2048)
s.close()
print(buf)


#running server:
$ ./server.py
127.0.0.1 - - [06/Oct/2013 17:33:41] code 400, message Bad HTTP/0.9 request type
 ('E)\xaeE^2¤\xf2W\x8f\xb3aG')
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 18234)
Traceback (most recent call last):
  File "c:\Python33\lib\socketserver.py", line 306, in _handle_request_noblock
    self.process_request(request, client_address)
  File "c:\Python33\lib\socketserver.py", line 332, in process_request
    self.finish_request(request, client_address)
  File "c:\Python33\lib\socketserver.py", line 345, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\Python33\lib\socketserver.py", line 666, in __init__
    self.handle()
  File "c:\Python33\lib\http\server.py", line 400, in handle
    self.handle_one_request()
  File "c:\Python33\lib\http\server.py", line 380, in handle_one_request
    if not self.parse_request():
  File "c:\Python33\lib\http\server.py", line 311, in parse_request
    "Bad HTTP/0.9 request type (%r)" % command)
  File "c:\Python33\lib\http\server.py", line 428, in send_error
    self.send_response(code, message)
  File "c:\Python33\lib\http\server.py", line 443, in send_response
    self.log_request(code)
  File "./server.py", line 5, in log_request
    print('header is',req.headers.get('X-Forwarder-For'),', code',code)
AttributeError: 'HNDL' object has no attribute 'headers'
----------------------------------------


#running client:
$ ./client.py
b''
$

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18229>
_______________________________________


More information about the Python-bugs-list mailing list