SimpleHTTPServer将default.htm default.html添加到索引文件 [英] SimpleHTTPServer add default.htm default.html to index files

查看:117
本文介绍了SimpleHTTPServer将default.htm default.html添加到索引文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是使用$ python -m SimpleHTTPServer进行快速本地静态Web测试,它非常适合index.htmindex.html作为索引文件.

I always use $ python -m SimpleHTTPServer for fast local static web testing, it works great with index.htm or index.html as index files.

但是,对于当前正在处理的项目,我需要使用default.htmdefault.html.有人可以帮忙编写一个简单的脚本吗?

However I need to use default.htm or default.html for the project I'm working on at the moment. Can someone help to write a simple script for it please?

我在网络上找到了以下示例,希望它可以对入门有所帮助.

I found the below sample on the web, I hope it could help a little to get started.

import sys, SimpleHTTPServer, BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

SimpleHTTPRequestHandler.protocol_version = "HTTP/1.0"

httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler)

sa = httpd.socket.getsockname()

print "Serving HTTP on", sa[0], sa[1], "..."

httpd.serve_forever()

推荐答案

这是一种方法:

import os
import sys
import SimpleHTTPServer
import BaseHTTPServer

class MyHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        if not self.redirect():
            SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_HEAD(self):
        if not self.redirect():
            SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD(self)

    def redirect(self):
        path = self.translate_path(self.path)
        if os.path.isdir(path):
            for base in "index", "default":
                for ext in ".html", ".htm", ".txt":
                    index = base+ext
                    index_path = os.path.join(path, index)
                    if os.path.exists(index_path):
                        new_path = self.path
                        if not new_path.endswith('/'):
                            new_path += '/'
                        new_path += index

                        self.send_response(302)
                        self.send_header("Location", new_path)
                        self.end_headers()
                        return True
        return False

def test(HandlerClass = MyHTTPRequestHandler,
         ServerClass = BaseHTTPServer.HTTPServer):
    BaseHTTPServer.test(HandlerClass, ServerClass)


if __name__ == '__main__':
    test()

这是另一种方式.

import os
import sys
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer

class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
    def translate_path(self,path):
        path = SimpleHTTPRequestHandler.translate_path(self,path)
        if os.path.isdir(path):
            for base in "index", "default":
                for ext in ".html", ".htm", ".txt":
                    index = path + "/" + base + ext
                    if os.path.exists(index):
                        return index
        return path

def test(HandlerClass = MyHTTPRequestHandler,
         ServerClass = BaseHTTPServer.HTTPServer):
    BaseHTTPServer.test(HandlerClass, ServerClass)


if __name__ == '__main__':
    test()

最后,这是一个HTTP和HTTPS软件包服务器,带有各种有用的参数.用-h运行此命令以查看帮助消息.

Finally, here is a package HTTP and HTTPS server, with various useful args. Run this with -h to see the help message.

#!/usr/bin/python2.7

import os
import sys
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer

class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
    def translate_path(self,path):
        path = SimpleHTTPRequestHandler.translate_path(self,path)
        if os.path.isdir(path):
            for base in "index", "default":
                for ext in ".html", ".htm", ".txt":
                    index = path + "/" + base + ext
                    if os.path.exists(index):
                        return index
        return path

def test(HandlerClass = MyHTTPRequestHandler,
         ServerClass = BaseHTTPServer.HTTPServer):

    import argparse
    parser = argparse.ArgumentParser(description='Dump ANT files')
    parser.add_argument('-p','--port',
                        type=int,
                        default=8080,
                        help='port number')
    parser.add_argument('-i','--ip',
                        default='',
                        help='IP address to listen on: "" means all')
    parser.add_argument('-d','--docroot',
                        default='.',
                        help='Directory to serve files from')
    parser.add_argument('-s','--https',
                        action='store_true',
                        help='Use HTTPS instead of HTTP')
    parser.add_argument('-c', '--certfile', help='server certificate file')
    parser.add_argument('-k', '--keyfile', help='private key file')
    args = parser.parse_args()
    if os.path.isdir(args.docroot):
        os.chdir(args.docroot)
    else:
        parser.error('Docroot must be a directory')

    proto = 'HTTP'
    server_address = (args.ip, args.port)
    httpd = ServerClass(server_address, HandlerClass)

    if args.https:
        import ssl
        if not args.certfile:
            parser.error('Certificate file must be specified')
        if not os.path.isfile(args.certfile):
            parser.error('Certificate file must exist')
        if not args.keyfile:
            parser.error('Private key file must be specified')
        if not os.path.isfile(args.keyfile):
            parser.error('Private key file must exist')
        httpd.socket = ssl.wrap_socket(
            httpd.socket, 
            server_side=True,
            certfile=args.certfile,
            keyfile=args.keyfile)
        proto = 'HTTPS'

    sa = httpd.socket.getsockname()
    print "Serving %s on %s port %s ..."%(proto, sa[0], sa[1])
    httpd.serve_forever()



if __name__ == '__main__':
    test()

这篇关于SimpleHTTPServer将default.htm default.html添加到索引文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆