我可以监听子域python 3.4 SimpleHTTPRequestHandler

时间:2014-07-15 11:38:12

标签: python python-3.x subdomain

在这里,我尝试获取有关服务器实例的更多信息。

#! C:\python34\python

import sys
from http.server import SimpleHTTPRequestHandler
from http.server import HTTPServer

if sys.argv[1:]: port = int(sys.argv[1])
else:            port = 8000

def expl(H):
    R = ''
    for item in H:
        R += "<br/> -> " +  str(item) +" :"+ str(H[item])
    return R

class MyHandler(SimpleHTTPRequestHandler):

    def __init__(self,req,client_addr,server):
        SimpleHTTPRequestHandler.__init__(self,req,client_addr,server)

    def do_GET(self):
        page_text = ""\
                  + "server_version: "+ self.server_version +"<br/>"\
                  + "sys_version: "+ self.sys_version +"<br/>"\
                  + "protocol_version: "+ self.protocol_version +"<br/>"\
                  + "version_string(): "+ str(self.version_string()) +"<br/>"\
                  + "address_string(): "+ self.address_string() +"<br/>"\
                  + "path: "+ self.path +"<br/>"\
                  + "command: "+ self.command +"<br/>"\
                  + "headers: "+ expl(self.headers) +"<br/>"\
                  + "server.server_name: "+ self.server.server_name +"<br/>"\
                  + "server.server_port: "+ str(self.server.server_port) +"<br/>"\
                  + "server.socket.getsockname(): "+ str(self.server.socket.getsockname()) +"<br/>"\
                  + "responses: "+ expl(self.responses) +"<br/>"
#Err                  + self.client_address +"<br/>"\
#Err                  + str(self.headers.getheaders('referer')) +"<br/>"\
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.send_header("Content-length", len( page_text ))
        self.end_headers()
        self.wfile.write( page_text.encode("utf-8") )

try:
    server_address = ('localhost', port)
    httpd = HTTPServer(server_address, MyHandler)
    sa = httpd.socket.getsockname()
    print("Serving HTTP on", sa[0], "port", sa[1], "...")
    httpd.serve_forever()
except KeyboardInterrupt:
    print('^C received, shutting down server')
    httpd.socket.close()

在浏览器上,您可以使用这些网址:

  • 本地主机:8000 /
  • 本地主机:8000 / foo.html

但不是:

  • sub.localhost:8000 /

我认为服务器不会监听任何子域名吗?

即使我试图改变这个:

server_address = ('localhost', port)

server_address = ('sub.localhost', port)

有人知道如何使用python监听子域吗?

0 个答案:

没有答案