From 53a286ee1564b4216d1d1d1bc447612dab343ced Mon Sep 17 00:00:00 2001 From: Sergey Chizhik Date: Sun, 4 Oct 2015 10:58:14 +0300 Subject: [PATCH 1/3] Add ability to specify port,host to run on --- httpbin/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httpbin/core.py b/httpbin/core.py index d422c59..bbac37d 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -13,6 +13,7 @@ import os import random import time import uuid +import sys from flask import Flask, Response, request, render_template, redirect, jsonify as flask_jsonify, make_response, url_for from werkzeug.datastructures import WWWAuthenticate, MultiDict @@ -711,4 +712,4 @@ def xml(): if __name__ == '__main__': - app.run() + app.run(port=int(sys.argv[1]) if (len(sys.argv) > 1) else 5000, host=sys.argv[2] if (len(sys.argv) > 2) else "127.0.0.1") From 936e49b4e9acd87a77a2bf75a5f4a0100b4824b0 Mon Sep 17 00:00:00 2001 From: Sergey Chizhik Date: Tue, 6 Oct 2015 17:58:10 +0300 Subject: [PATCH 2/3] sys > argparse, fix readme --- README.rst | 2 +- httpbin/core.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index c9d2b10..7e8cb19 100644 --- a/README.rst +++ b/README.rst @@ -189,7 +189,7 @@ Or run it directly: .. code:: bash - $ python -m httpbin.core + $ python -m httpbin.core [--port=PORT] [--host=HOST] Changelog --------- diff --git a/httpbin/core.py b/httpbin/core.py index bbac37d..3f53b77 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -13,7 +13,7 @@ import os import random import time import uuid -import sys +import argparse from flask import Flask, Response, request, render_template, redirect, jsonify as flask_jsonify, make_response, url_for from werkzeug.datastructures import WWWAuthenticate, MultiDict @@ -712,4 +712,8 @@ def xml(): if __name__ == '__main__': - app.run(port=int(sys.argv[1]) if (len(sys.argv) > 1) else 5000, host=sys.argv[2] if (len(sys.argv) > 2) else "127.0.0.1") + parser = argparse.ArgumentParser() + parser.add_argument("--port", type=int) + parser.add_argument("--host") + args = parser.parse_args() + app.run(port=args.port if args.port else 5000, host=args.host if args.host else "127.0.0.1") From 2b83b98fbf909c08c6edddca48e5660ee4ef3358 Mon Sep 17 00:00:00 2001 From: Sergey Chizhik Date: Tue, 6 Oct 2015 18:57:07 +0300 Subject: [PATCH 3/3] Default value instead of ternaries --- httpbin/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/httpbin/core.py b/httpbin/core.py index 3f53b77..ff3a79b 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -713,7 +713,7 @@ def xml(): if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument("--port", type=int) - parser.add_argument("--host") + parser.add_argument("--port", type=int, default=5000) + parser.add_argument("--host", default="127.0.0.1") args = parser.parse_args() - app.run(port=args.port if args.port else 5000, host=args.host if args.host else "127.0.0.1") + app.run(port=args.port, host=args.host)