• Python 简易 HTTP 服务器


    Python 简易 HTTP 服务器(http.server)

    http.server 是 socketserver.TCPServer 的子类,它在 HTTP 套接字上创建和监听,并将请求分派给处理程序。

    Python3 内置标准模块。
    通过执行如下命令创建一个最简单的 HTTP 服务器:

    python -m http.server # 服务器默认监听端口是 8000
    python -m http.server 9000 # 支持自定义端口号
    python -m http.server --bind 127.0.0.1 # 服务器默认绑定到所有接口,可以通过 -b/--bind 指定地址,如本地主机。
    python -m http.server --directory /tmp/ # 服务器默认工作目录为当前目录,可通过 -d/--directory 参数指定工作目录。
    python -m http.server --cgi # 可以通过传递参数 --cgi 启用 CGI 请求处理程序:
    
    • 1
    • 2
    • 3
    • 4
    • 5

    http.server 也支持在代码中调用,导入对应的类和函数即可。

    from http.server import SimpleHTTPRequestHandler
    from http.server import CGIHTTPRequestHandler
    from http.server import ThreadingHTTPServer
    from functools import partial
    import contextlib
    import sys
    import os
    
    class DualStackServer(ThreadingHTTPServer):
        def server_bind(self):
            # suppress exception when protocol is IPv4
            with contextlib.suppress(Exception):
                self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
            return super().server_bind()
    
    
    def run(server_class=DualStackServer,
            handler_class=SimpleHTTPRequestHandler,
            port=8000,
            bind='127.0.0.1',
            cgi=False,
            directory=os.getcwd()):
        """Run an HTTP server on port 8000 (or the port argument).
    
        Args:
            server_class (_type_, optional): Class of server. Defaults to DualStackServer.
            handler_class (_type_, optional): Class of handler. Defaults to SimpleHTTPRequestHandler.
            port (int, optional): Specify alternate port. Defaults to 8000.
            bind (str, optional): Specify alternate bind address. Defaults to '127.0.0.1'.
            cgi (bool, optional): Run as CGI Server. Defaults to False.
            directory (_type_, optional): Specify alternative directory. Defaults to os.getcwd().
        """
    
        if cgi:
            handler_class = partial(CGIHTTPRequestHandler, directory=directory)
        else:
            handler_class = partial(SimpleHTTPRequestHandler, directory=directory)
    
        with server_class((bind, port), handler_class) as httpd:
            print(
                f"Serving HTTP on {bind} port {port} "
                f"(http://{bind}:{port}/) ..."
            )
            try:
                httpd.serve_forever()
            except KeyboardInterrupt:
                print("\nKeyboard interrupt received, exiting.")
                sys.exit(0)
    
    if __name__ == '__main__':
        run(port=8000, bind='127.0.0.1')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    server_class:服务器类
    handler_class:请求处理类
    port:端口
    bind:IP
    cgi:是否启用 CGI 请求处理程序
    directory:工作目录
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    小型 web 项目在局域网内的预览
    项目目录

    web:.
    ├─index.html
    
    • 1
    • 2
    index.html
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Documenttitle>
    head>
    <body>
        hello world
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    切换到目录 cd web,执行命令 python -m http.server,浏览器地址栏输入 localhost:8000,显示:hello world

    对于局域网的其他用户,可通过你的主机 IP + 端口号访问,如你的主机 IP是192.168.0.1,通过网址 192.168.0.1:8000 可以看到 index.html 文件渲染的内容。

    在本地浏览器访问远程服务器的端口映射
    如果通过 VSCode 连接远程服务器,使用 http.server 开启一个端口后,会自动映射到本地,这样在本地浏览器就能查看和下载远程服务器资源。(除 VSCode 外,其他工具应该也可以实现远程与本地的端口映射)

    注意
    http.server 只实现了最基本的安全审查,请不要用于生产环境。

  • 相关阅读:
    力扣:22-括号生成
    MVC第三波书店缓存帮助类RedisHelper
    dos2unix命令
    二叉搜索树
    《腾讯精选练习50题》专题
    indexDB & localForage
    1143 Lowest Common Ancestor
    电路设计(26)——速度表的multisim仿真
    INSERT ... ON DUPLICATE KEY UPDATE
    pthread 常用 API 创建 销毁 调度 互斥锁 自旋锁 读写锁 条件变量
  • 原文地址:https://blog.csdn.net/weixin_43955170/article/details/134475509