本文介绍了确定BaseHTTPServer中的站点域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在基于HTTPServer的python上实现简单的服务器。
如何提取当前请求中提供的站点域的信息?
我的意思是它可以服务于几个域,如site1.com和site2.com,例如,我该如何得到这个代码:

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()


推荐答案

我想你应该可以读取主机头。
可以从BaseHTTPRequestHandler.headers访问头文件

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

这篇关于确定BaseHTTPServer中的站点域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 11:50