龙空技术网

使用 Python 启动简易的 http 服务器

DevOps砖工 182

前言:

此时姐妹们对“nginxautoindex403”大体比较着重,同学们都需要分析一些“nginxautoindex403”的相关文章。那么小编在网摘上收集了一些对于“nginxautoindex403””的相关资讯,希望朋友们能喜欢,咱们一起来学习一下吧!

在 Python 中启用简易文件服务器

在Python中可以通过两种方式创建web服务器,第一类是开箱即用的,直接命令行启动;第二种自定义一些功能。

目标:实现简易的http下载服务器,能在Windows或Linux一键启动。

1、支持自定义映射指定目录到HTTP请求路径,而不仅限于脚本所在目录;

2、重写日志调用方法,支持终端不同颜色显示日志,且日志支持写入文件;

3、实现类Nginx autoindex off功能,即关闭目录浏览功能,仅可访问文件下载;

4、默认单线程访问,一个文件被下载时,其他人无法同时下载,使用 ThreadingTCPServer 实现多线程功能;

5、点击文件下载立即取消的异常日志记录;

命令行启动 http 服务器

python -m SimpleHTTPServer 9999

如果出现 No Module named SimpleHTTPServer

看一下python版本,python --version,如果是python3的话,可以试试

python -m http.server 9999python -m http.server 8899 --directory /path
启动 http 服务器

最简单的几行代码

这个启动和命令行比较类似。

import http.serverimport socketserverPORT = 8000 # 端口号Handler = http.server.SimpleHTTPRequestHandler# class http.server.SimpleHTTPRequestHandler(request, client_address, server, directory=None)with socketserver.TCPServer(("", PORT), Handler) as httpd:    print("serving at port", PORT)    httpd.serve_forever()
自定义一些功能方法
#!/usr/bin/python# -*- coding: utf-8 -*-import sysimport osimport datetimefrom urllib import parsefrom http.server import SimpleHTTPRequestHandlerfrom socketserver import TCPServer, ThreadingTCPServerfrom log import get_logger  # 使用 loguru 记录日志svr_port = 8000  # 端口号root_dir = 'C:\\Users\\admin\\Desktop'  # 共享的路径show_dir = True  # 是否开启目录浏览功能log_path = 'simple_http.log'  # 日志文件存放路径my_logger = get_logger(log_path)class CustomHTTPRequestHandler(SimpleHTTPRequestHandler):    def __init__(self, *args, **kwargs):        """自定义本地路径"""        super().__init__(*args, directory=root_dir, **kwargs)    def log_request(self, code='-', size='-'):        """        记录接受的请求,这是由 send_response() 调用的。        重写方法: 调用 log_message_new() 实现自定义日志显示。        """        from http import HTTPStatus        if isinstance(code, HTTPStatus):            code = code.value        # self.log_message('"%s" %s %s', self.requestline, str(code), str(size))        if code in range(100, 200):            level = 10  # INFO        elif code in range(200, 400):            level = 20  # SUCCESS        else:            level = 30  # WARNING        msg = f'"{self.requestline}" {code} {size}'        self.log_message_new(msg, level)    def log_error(self, format_str, *args):        """        记录错误。        重写方法: 调用 log_message_new() 实现自定义日志显示。        """        # self.log_message(format_str, *args)        level = 40  # ERROR        self.log_message_new(format_str, level, *args)    def log_message(self, format_str, *args):        """原始日志方法"""        now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")        sys.stderr.write(f"{self.address_string()} - - [{now_time}] {format_str % args}\n")        """        127.0.0.1 - - [2023-02-16 10:18:50] "GET /Apps/Python/Python38/python.exe HTTP/1.1" 200 -        127.0.0.1 - - [2023-02-16 10:25:40] code 404, message File not found        127.0.0.1 - - [2023-02-16 10:25:40] "GET /Apps/Python/Python38/12 HTTP/1.1" 404 -        """    def log_message_new(self, format_str, level, *args):        """        接入自定义的日志显示格式。        支持显示到终端和记录到日志文件。        """        # print(format_str, parse.unquote(format_str, encoding='utf-8'))        # 文件夹中文时,%E5%AE%89%E8%A3%85%E5%8C%85 “format_str % args” 报错,需把 %xx 转义字符替换成字符,及还原成中文        format_str = parse.unquote(format_str, encoding='utf-8')        msg = f'{self.address_string()} {format_str % args}'        if level == 10:            my_logger.info(msg)        elif level == 20:            my_logger.success(msg)        elif level == 30:            my_logger.warning(msg)        else:            my_logger.error(msg)    def do_GET(self):        """        发送GET请求。        重写方法:实现 Nginx 相似打开目录浏览得功能        """        path = self.translate_path(self.path)        if self.path.endswith('favicon.ico'):            # "GET /favicon.ico HTTP/1.1" 404 报错隐藏            return        # 如果是目录,且 show_dir 关闭,则直接放回403        if os.path.isdir(path) and show_dir is False:            # 方法一:使用自带的页面,传输参数            from http import HTTPStatus            self.send_error(HTTPStatus.FORBIDDEN, "Forbidden, directory is not allowed to be browsed")            return            # 方法二:自定义页面            # self.send_response(403)            # self.end_headers()            # html_403 = """            # <html>            #     <head>            #         <meta http-equiv="Content-Type" content="text/html;charset=utf-8">            #         <title>Simple Web 403</title>            #     </head>            #     <body>            #         <center><h1>403 Forbidden 目录不支持浏览</h1></center>            #         <hr><center>Simple Web</center>            #     </body>            # </html>            # """            # self.wfile.write(html_403.encode('utf-8', 'replace'))            # return        else:            # self.log_message_new(f'is file: {path}', 10)            pass        try:            super().do_GET()        except ConnectionResetError as e:            # 点击链接下载文件但又取消:ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。            my_logger.exception(f'连接中断:{e}')        except ConnectionAbortedError as e:            # 取消下载的文件点击重试但又取消:ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。            my_logger.exception(f'连接中断:{e}')if __name__ == '__main__':    # 默认路径:使用脚本所在目录    # handler = SimpleHTTPRequestHandler    # 自定义路径    handler = CustomHTTPRequestHandler    # 单线程    # httpd = TCPServer(server_address=("", svr_port), RequestHandlerClass=handler)    # 多线程,不影响多人下载同一个文件    httpd = ThreadingTCPServer(server_address=("", svr_port), RequestHandlerClass=handler)    try:        my_logger.info(f"Server started at port {svr_port}. Press CTRL+C to close the server.")        httpd.serve_forever()    except KeyboardInterrupt:        httpd.server_close()        my_logger.info("Server closed")    finally:        httpd.server_close()        my_logger.info("Server closed finally")
单线程多线程下载对比使用 TCPServer 单线程下载文件,对于同一个文件,需要等上一个人下载完成后,才能响应另一个人的下载使用 ThreadingTCPServer 多线程,同一个文件支持同时下载,见上图从日志中可以查看到,多线程将有多个线程提供访问
# 单线程下载2023-02-17 11:43:14 | INFO     | MainProcess:1288 | MainThread:7360 | __main__.test_log.<module>:176 - Server started at port 8000. Press CTRL+C to close the server.2023-02-17 11:43:17 | SUCCESS  | MainProcess:1288 | MainThread:7360 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET / HTTP/1.1" 200 -2023-02-17 11:43:18 | SUCCESS  | MainProcess:1288 | MainThread:7360 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET /安装包/ HTTP/1.1" 200 -2023-02-17 11:43:24 | ERROR    | MainProcess:1288 | MainThread:7360 | __main__.test_log.log_message_new:122 - 192.168.168.1 code 404, message File not found2023-02-17 11:43:24 | WARNING  | MainProcess:1288 | MainThread:7360 | __main__.test_log.log_message_new:120 - 192.168.168.1 "GET /安装包/test.txt HTTP/1.1" 404 -2023-02-17 11:43:32 | SUCCESS  | MainProcess:1288 | MainThread:7360 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET /安装包.zip HTTP/1.1" 200 -2023-02-17 11:43:47 | SUCCESS  | MainProcess:1288 | MainThread:7360 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET /安装包.zip HTTP/1.1" 200 -2023-02-17 11:44:11 | INFO     | MainProcess:1288 | MainThread:7360 | __main__.test_log.<module>:180 - Server closed2023-02-17 11:44:11 | INFO     | MainProcess:1288 | MainThread:7360 | __main__.test_log.<module>:183 - Server closed finally# 多线程下载2023-02-17 11:44:57 | INFO     | MainProcess:428 | MainThread:2904 | __main__.test_log.<module>:176 - Server started at port 8000. Press CTRL+C to close the server.2023-02-17 11:45:01 | SUCCESS  | MainProcess:428 | Thread-1:6388 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET / HTTP/1.1" 200 -2023-02-17 11:45:04 | SUCCESS  | MainProcess:428 | Thread-4:5688 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET /安装包/ HTTP/1.1" 200 -2023-02-17 11:45:06 | SUCCESS  | MainProcess:428 | Thread-10:4808 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET /安装包.zip HTTP/1.1" 200 -2023-02-17 11:45:12 | SUCCESS  | MainProcess:428 | Thread-11:3972 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET /安装包.zip HTTP/1.1" 200 -2023-02-17 11:45:21 | SUCCESS  | MainProcess:428 | Thread-12:4612 | __main__.test_log.log_message_new:118 - 192.168.168.1 "GET /安装包.zip HTTP/1.1" 200 -2023-02-17 11:46:12 | INFO     | MainProcess:428 | MainThread:2904 | __main__.test_log.<module>:180 - Server closed2023-02-17 11:46:12 | INFO     | MainProcess:428 | MainThread:2904 | __main__.test_log.<module>:183 - Server closed finally
目录浏览功能开关关闭目录浏览功能 show_dir = False ,访问目录会提示403,而直接下载文件不影响
2023-02-17 16:03:07 |     INFO: Server started at port 8000. Press CTRL+C to close the server.2023-02-17 16:03:12 |    ERROR: 192.168.168.1 code 403, message Forbidden, directory is not allowed to be browsed2023-02-17 16:03:12 |  WARNING: 192.168.168.1 "GET / HTTP/1.1" 403 -
下载文件中断异常捕获点击链接下载文件但又取消:ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
2023-02-17 16:03:14 |  SUCCESS: 192.168.168.1 "GET /安装包.zip HTTP/1.1" 200 -2023-02-17 16:03:16 |    ERROR: 连接中断:[WinError 10053] 你的主机中的软件中止了一个已建立的连接。
取消下载的文件点击重试但又取消:ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。
2023-02-17 16:03:43 |  SUCCESS: 192.168.168.1 "GET /安装包.zip HTTP/1.1" 200 -2023-02-17 16:03:45 |    ERROR: 连接中断:[WinError 10054] 远程主机强迫关闭了一个现有的连接。

标签: #nginxautoindex403 #apachehttpclient43 #python 启动服务器 #html连接服务器 #nginx加载python