This looks horrible, and I'm not sure if this is the best way to do this, but the following code appears to start the server without too much difficulty:
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__, 'exec'), 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__, 'exec'), globals, locals)
import http.server
exec_module(http.server, dict(__name__='__main__'))
2: exec_module , , , - , , :
def exec_module(module, globals=None, 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__),
module.__file__, 'exec'), exec_globals, exec_locals)
. -, , . -, , .