2010-06-02 50 views
0

我嘗試在基於HTTPServer的python上實現簡單的服務器。 如何提取有關當前請求中提供的網站域的信息? 我的意思是可以SERV幾個領域,如site1.com和site2.com例如,我怎樣才能得到它在此代碼:在BaseHTTPServer中確定站點域

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

我想你應該能夠讀取主機頭。 可以從BaseHTTPRequestHandler.headers訪問頭文件

+0

謝謝,我解決了它: [domain] = re.findall('Host:(。*?)\ r \ n',str(self.headers)) – Evg 2010-06-02 15:40:59

相關問題