6

i have a simple python cgi server:

import BaseHTTPServer import CGIHTTPServer import cgitb; cgitb.enable() ## This line enables CGI error reporting server = BaseHTTPServer.HTTPServer handler = CGIHTTPServer.CGIHTTPRequestHandler server_address = ("", 8000) httpd = server(server_address, handler) httpd.serve_forever() 

the server does a reverse dns lookup on every request for logging purposes onto the screen. there is no dns server available as i am running the server in a local network setting. so every reverse dns lookup leads to lookup timeout, delaying the server's response. how can i disable the dns lookup? i didn't find an answer in the python docs.

1 Answer 1

12

You can subclass your own handler class, which won't do the DNS lookups. This follows from http://docs.python.org/library/cgihttpserver.html#module-CGIHTTPServer which says CGIHTTPRequestHandler is interface compatible with BaseHTTPRequestHandler and BaseHTTPRequestHandler has a method address_string().

class MyHandler(CGIHTTPServer.CGIHTTPRequestHandler): # Disable logging DNS lookups def address_string(self): return str(self.client_address[0]) handler = MyHandler 
Sign up to request clarification or add additional context in comments.

1 Comment

Neat. I sometimes find it useful to quickly spin up a HTTP server using just "python -m SimpleHTTPServer". Here's a one-liner that does the same thing but uses the above tip to disable the reverse DNS lookups. python -c "import SimpleHTTPServer; SimpleHTTPServer.SimpleHTTPRequestHandler.address_string = lambda self: str(self.client_address[0]); SimpleHTTPServer.test()"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.