加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在python中启动SimpleHTTPServer

发布时间:2020-12-20 13:48:23 所属栏目:Python 来源:网络整理
导读:如果__name__ ==’__ main__’比复制/过去更好,是否有一种更优雅/系统/未来的方法来调用所有代码? 我正在将我的bash脚本转换为python.我们大量使用的两个命令是python -m SimpleHTTPServer YOUR_PORT和python -m http.server YOUR_PORT. 在2.x中翻译它是相
如果__name__ ==’__ main__’比复制/过去更好,是否有一种更优雅/系统/未来的方法来调用所有代码?

我正在将我的bash脚本转换为python.我们大量使用的两个命令是python -m SimpleHTTPServer YOUR_PORT和python -m http.server YOUR_PORT.

在2.x中翻译它是相当干净的.

SimpleHTTPServer main:

if __name__ == '__main__':
    test()

我的代码模拟main:

import SimpleHTTPServer
SimpleHTTPServer.test()

在3.x中翻译这个并不干净.

http.server main:

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--cgi',action='store_true',help='Run as CGI Server')
    parser.add_argument('--bind','-b',default='',metavar='ADDRESS',help='Specify alternate bind address '
                             '[default: all interfaces]')
    parser.add_argument('port',action='store',default=8000,type=int,nargs='?',help='Specify alternate port [default: 8000]')
    args = parser.parse_args()
    if args.cgi:
        handler_class = CGIHTTPRequestHandler
    else:
        handler_class = SimpleHTTPRequestHandler
    test(HandlerClass=handler_class,port=args.port,bind=args.bind)

我的代码模拟main:

import argparse
import http.server
from http.server import CGIHTTPRequestHandler,SimpleHTTPRequestHandler
parser = argparse.ArgumentParser()
parser.add_argument('--cgi',help='Run as CGI Server')
parser.add_argument('--bind',help='Specify alternate bind address '
                         '[default: all interfaces]')
parser.add_argument('port',help='Specify alternate port [default: 8000]')
args = parser.parse_args()
if args.cgi:
    handler_class = CGIHTTPRequestHandler
else:
    handler_class = SimpleHTTPRequestHandler
http.server.test(HandlerClass=handler_class,bind=args.bind)

解决方法

这看起来很糟糕,我不确定这是否是最好的方法,但以下代码似乎启动服务器没有太多困难:

import importlib
exec(compile(importlib.util.find_spec('http.server').loader.get_source(
    'http.server'),'server.py','exec'),dict(__name__='__main__'))

更新1:启动http.server模块的下一个想法可能不是更好,但它直接利用http.server模块而不必处理importlib模块.

import http.server
exec(compile(http.server.__loader__.get_source(http.server.__name__),http.server.__file__,dict(__name__='__main__'))

当然,这可能只是意味着创建一个实用程序函数会更好,它可以在我们想要运行它们的任何上下文中处理执行模块.

import sys

nvl = lambda value,other: other if value is None else value

def exec_module(module,globals=None,locals=None):
    frame = sys._getframe(1)
    globals = nvl(globals,{})
    globals.update(frame.f_globals)
    locals = nvl(locals,{})
    locals.update(frame.f_locals)
    exec(compile(module.__loader__.get_source(module.__name__),module.__file__,globals,locals)

# this is how you would use the code up above

import http.server

exec_module(http.server,dict(__name__='__main__'))

更新2:exec_module函数应该更像下面的内容,但由于某些原因未能引起我的注意,它似乎没有按预期工作:

def exec_module(module,locals=None):
    frame = sys._getframe(1)

    exec_globals = nvl(globals,{})
    copy_globals = exec_globals.copy()
    exec_globals.update(frame.f_globals)
    exec_globals.update(copy_globals)

    exec_locals = nvl(locals,{})
    copy_locals = exec_locals.copy()
    exec_locals.update(frame.f_locals)
    exec_locals.update(copy_locals)

    exec(compile(module.__loader__.get_source(module.__name__),exec_globals,exec_locals)

这些变化考虑了两件事.首先,如果调用者想要在函数运行后检查它们的状态,则保留对传入的全局变量和局部变量的引用.其次,调用者的全局变量和局部变量不能覆盖已传入的全局变量和局部变量中已设置的任何值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读