{"id":3888,"date":"2024-04-11T18:29:04","date_gmt":"2024-04-11T18:29:04","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3888"},"modified":"2024-04-11T18:29:04","modified_gmt":"2024-04-11T18:29:04","slug":"a-webserver-from-the-command-line","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3888","title":{"rendered":"A webserver from the command line"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Python has a module (Flask) that allows to run a Python module from the commmand line. The code looks like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># import main Flask class and request object\nfrom flask import Flask, request\n\n# create the Flask app\napp = Flask(__name__)\n\n@app.route('\/form-example', methods=['GET', 'POST'])\ndef form_example():\n    if request.args.get('new') == 'NEW':\n        return '''\n                &lt;form method=\"POST\" action =\"http:\/\/127.0.0.1:5000\/form-example\">\n                        &lt;div>&lt;label>Language: &lt;input type=\"text\" name=\"language\">&lt;\/label>&lt;\/div>\n                        &lt;div>&lt;label>Framework: &lt;input type=\"text\" name=\"framework\">&lt;\/label>&lt;\/div>\n                        &lt;input type=\"submit\" value=\"Submit\">\n                    &lt;\/form>'''\n    language = request.form.get('language')\n    framework = request.form.get('framework')\n    return '''\n                &lt;h1>The language value is: {}&lt;\/h1>\n                &lt;h1>The framework value is: {}&lt;\/h1>'''.format(language, framework)\n           \n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This can be run from the command line with: python index.py, which leads to a service that is started.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-11-202101.png\" alt=\"\" class=\"wp-image-3889\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Look at the code. If we provide an URL like &#8220;http:\/\/127.0.0.1:5000\/form-example?new=NEW&#8221;, we see that two input fields are provided:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-11-202530.png\" alt=\"\" class=\"wp-image-3890\"\/><figcaption class=\"wp-element-caption\">If the submit button is pushed, we see that URL &#8220;http:\/\/127.0.0.1:5000\/form-example&#8221; is used. This provides <\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-11-202833.png\" alt=\"\" class=\"wp-image-3891\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Python has a module (Flask) that allows to run a Python module from the commmand line. The code looks like: # import main Flask class and request object from flask import Flask, request # create the Flask app app = Flask(__name__) @app.route(&#8216;\/form-example&#8217;, methods=[&#8216;GET&#8217;, &#8216;POST&#8217;]) def form_example(): if request.args.get(&#8216;new&#8217;) == &#8216;NEW&#8217;: return &#8221;&#8217; &lt;form method=&#8221;POST&#8221; action [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-3888","post","type-post","status-publish","format-standard","hentry","category-niet-gecategoriseerd"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3888"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3888\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3888"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}