开发者

Getting Host field from HTTP request in BaseHTTPRequestHandler

开发者 https://www.devze.com 2023-02-28 10:02 出处:网络
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) l

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 t开发者_开发知识库hat.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号