views:

24

answers:

1

I try to implement simple server on python based on HTTPServer. How can i extract information about site domain served in current request? I mean it can serv several domains such as site1.com and site2.com for example, how can i get it in this code:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        print "get"
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        #how can i get here host name of serving site?
        #site1.com or site2.com ?
        domain = ???
        self.wfile.write('<html>Welcome on www.%s.com</html>' % (domain))



if __name__ == "__main__":
    try:
        server = HTTPServer(("", 8070), MyHandler)
        print "started httpserver..."
        server.serve_forever()
    except KeyboardInterrupt:
        print "^C received, shutting down server"
        server.socket.close()
+1  A: 

I guess you should be able to read the Host header. The headers can be accessed from BaseHTTPRequestHandler.headers

baloo
thank you, i solve it :[domain] = re.findall('Host: (.*?)\r\n',str(self.headers))
Evg