Run SimpleHTTPServer in python

Is there a more elegant / systematic / future way of proving all the code in if __name__ == '__main__'which is better than copy / past?

I am converting my bash scripts to python. We use two commands: python -m SimpleHTTPServer YOUR_PORTand python -m http.server YOUR_PORT.

Translating this to 2.x is pretty clean.

SimpleHTTPServer main :

if __name__ == '__main__':
    test()

My code for modeling main:

import SimpleHTTPServer
SimpleHTTPServer.test()

Translating this to 3.x is not clean.

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)

My code for modeling main:

import argparse
import http.server
from http.server import CGIHTTPRequestHandler, SimpleHTTPRequestHandler
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
http.server.test(HandlerClass=handler_class, port=args.port, bind=args.bind)
+4
source share
1 answer

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)

# 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, 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)

. -, , . -, , .

+1

All Articles