Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aiohttp.web command-line interface to serve apps #740

Merged
merged 7 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move web_main.py content to web.py
  • Loading branch information
jashandeep-sohi committed Jan 17, 2016
commit 3fbf1065c57b323087b025d8212bdac688dbafcd
44 changes: 43 additions & 1 deletion aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from .web_exceptions import * # noqa
from .web_urldispatcher import * # noqa
from .web_ws import * # noqa
from argparse import ArgumentParser
from importlib import import_module


__all__ = (web_reqrep.__all__ +
web_exceptions.__all__ +
Expand Down Expand Up @@ -322,6 +325,45 @@ def run_app(app, *, host='0.0.0.0', port=None,
loop.run_until_complete(app.cleanup())
loop.close()


def main():
arg_parser = ArgumentParser(
description="aiohttp.web TCP/IP Application server",
prog="aiohttp.web"
)
arg_parser.add_argument(
"entry_func",
help=("Callable returning the `aiohttp.web.Application` instance to "
"run. Should be specified in the Python import syntax, "
"e.g. 'package.module.function')"),
metavar="entry-func"
)
arg_parser.add_argument(
"-n", "--hostname",
help="TCP/IP hostname to serve on (default: %(default)r)",
default="localhost"
)
arg_parser.add_argument(
"-p", "--port",
help="TCP/IP port to serve on (default: %(default)r)",
type=int,
default="8080"
)
args, extra_args = arg_parser.parse_known_args()

# Import logic
mod_str, _, func_str = args.entry_func.rpartition(".")
try:
module = import_module(mod_str)
func = getattr(module, func_str)
except (ImportError, ValueError) as e:
arg_parser.error(e)
except AttributeError as e:
arg_parser.error(e)

app = func(extra_args)
run_app(app, host=args.hostname, port=args.port)
arg_parser.exit(message="Stopped\n")

if __name__ == "__main__":
from .web_main import main
main()
43 changes: 0 additions & 43 deletions aiohttp/web_main.py

This file was deleted.