10

I'm writing a script using BaseHTTPRequestHandler class. And in do_GET(self) method I need to get the content of the Host field from the HTTP request. I can do it by regexping the str(self.headers) like proposed here: Determine site domain in BaseHTTPServer, but it's kinda ugly and I wonder if there's cleaner way to do that.

1 Answer 1

24

The attribute self.headers is a dictionary-like structure, so you can do this:

def do_GET(self): host = self.headers.get('Host') 

When the header does not exist, None is returned.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.