我尝试在基于HTTPServer的python上实现简单的服务器。 如何提取有关当前请求中提供的站点域的信息? 我的意思是它可以服务于几个域,例如site1.com和site2.com,我怎么能在这段代码中得到它:
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()
答案 0 :(得分:1)
我猜你应该能够读取Host标头。 可以从BaseHTTPRequestHandler.headers
访问标头