{"id":19128,"date":"2026-09-17T09:26:52","date_gmt":"2026-09-17T07:26:52","guid":{"rendered":"https:\/\/www.beseit.net\/?p=19128"},"modified":"2026-09-17T10:03:09","modified_gmt":"2026-09-17T08:03:09","slug":"%f0%9f%93%9d-rebotar-un-missatge-de-text-enviat-per-lentrada-dun-navegador-sobre-un-navegador-virtual","status":"publish","type":"post","link":"https:\/\/www.beseit.net\/?p=19128","title":{"rendered":"\ud83d\udcdd Rebotar un missatge de text enviat per l\u2019entrada d\u2019un navegador sobre un navegador virtual"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">En aquesta entrada veurem com crear un petit servidor local en Python capa\u00e7 de rebre text enviat des d\u2019un navegador i retornar-lo immediatament al mateix navegador. \u00c9s un exemple senzill per\u00f2 molt \u00fatil per entendre com funcionen les peticions HTTP, els formularis HTML i la comunicaci\u00f3 amb un servidor local.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Formulari per enviar el text:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html lang=\"ca\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;title>Enviar text al servidor Python&lt;\/title>\n&lt;\/head>\n&lt;body>\n    &lt;h1>Enviar text al servidor local Python&lt;\/h1>\n    &lt;form action=\"http:\/\/127.0.0.1:5000\/processa\" method=\"post\">\n        &lt;label for=\"text\">Text:&lt;\/label>\n        &lt;input type=\"text\" id=\"text\" name=\"text\">\n        &lt;button type=\"submit\">Enviar&lt;\/button>\n    &lt;\/form>\n&lt;\/body>\n&lt;\/html>\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 Preparar l\u2019entorn: Python i Flask<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Deepin 25 (o qualsevol distribuci\u00f3 basada en Debian), <code>pip<\/code> no permet instal\u00b7lar paquets directament al sistema. Per aix\u00f2 cal crear un <strong>entorn virtual<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Instal\u00b7lar <code>venv<\/code> (si cal)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install python3-venv\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Crear l\u2019entorn virtual<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 -m venv ~\/flask-env\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Activar-lo<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">source ~\/flask-env\/bin\/activate\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">El terminal hauria de mostrar:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Codi<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">((flask-env))\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Instal\u00b7lar Flask dins l\u2019entorn<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install flask\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 Crear el servidor local en Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Creem un fitxer anomenat <mark style=\"background-color:rgba(0, 0, 0, 0);color:#f20d0d\" class=\"has-inline-color\"><code>server.py<\/code> <\/mark>amb (nano server.py) i enganxa:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">python<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from flask import Flask, request\n\napp = Flask(__name__)\n\n@app.route('\/processa', methods=['POST'])\ndef processa():\n    text = request.form.get('text', '')\n    return f\"&lt;h1>Text rebut:&lt;\/h1>&lt;p>{text}&lt;\/p>\"\n\n@app.route('\/')\ndef index():\n    return \"\"\"\n    &lt;form action=\"\/processa\" method=\"post\">\n        &lt;input type=\"text\" name=\"text\">\n        &lt;button type=\"submit\">Enviar&lt;\/button>\n    &lt;\/form>\n    \"\"\"\n\nif __name__ == '__main__':\n    app.run(debug=True)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Aquest servidor fa dues coses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Serveix un formulari HTML senzill a <code>\/<\/code><\/li>\n\n\n\n<li>Rep el text enviat a <code>\/processa<\/code> i el retorna dins d\u2019una p\u00e0gina HTML<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 Executar el servidor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Amb l\u2019entorn virtual activat:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python server.py\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Si tot va b\u00e9, apareixer\u00e0:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Codi<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> <code>* Running on http:\/\/127.0.0.1:5000\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4\ufe0f\u20e3 Enviar text des del navegador<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ara nom\u00e9s cal obrir:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Codi<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">http:\/\/127.0.0.1:5000\/\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Escrius un text (per exemple, <strong>bon dia<\/strong>) i prems <strong>Enviar<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">El navegador fa:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Una petici\u00f3 <strong>POST<\/strong> a <code>\/processa<\/code><\/li>\n\n\n\n<li>Flask rep el text<\/li>\n\n\n\n<li>Flask genera una resposta HTML amb el text rebut<\/li>\n\n\n\n<li>El navegador mostra la resposta<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">5\ufe0f\u20e3 Diagrama del flux<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Codi<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502        Navegador         \u2502\n\u2502  Usuari escriu \"bon dia\" \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n              \u2502 POST \/processa\n              \u25bc\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502        Flask (Python)    \u2502\n\u2502  processa() rep el text  \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n              \u2502 Genera HTML\n              \u25bc\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502        Navegador         \u2502\n\u2502  Mostra \"Text rebut:     \u2502\n\u2502          bon dia\"        \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6\ufe0f\u20e3 Qu\u00e8 hem aconseguit?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Crear un entorn Python a\u00efllat<\/li>\n\n\n\n<li>Instal\u00b7lar Flask correctament<\/li>\n\n\n\n<li>Crear un servidor local funcional<\/li>\n\n\n\n<li>Enviar text des d\u2019un navegador<\/li>\n\n\n\n<li>Rebotar el text i mostrar-lo al navegador<\/li>\n\n\n\n<li>Validar el flux complet amb un exemple real<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Aquest \u00e9s el primer pas per integrar funcionalitats m\u00e9s avan\u00e7ades, com enviar el text a un sistema TTS (Piper) i reproduir-lo autom\u00e0ticament.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Si vols, puc preparar <strong>la segona part del post<\/strong>: \ud83d\udc49 <em>\u201cEnviar el text del navegador a Piper (Docker) i reproduir-lo com a veu Ona\u201d<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Nom\u00e9s cal que m\u2019ho diguis.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En aquesta entrada veurem com crear un petit servidor local en Python capa\u00e7 de rebre text enviat des d\u2019un navegador i retornar-lo immediatament al mateix navegador. \u00c9s un exemple senzill per\u00f2 molt \u00fatil per entendre com funcionen les peticions HTTP, &hellip; <a href=\"https:\/\/www.beseit.net\/?p=19128\">Continua llegint <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":3167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[207,1],"tags":[],"class_list":["post-19128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-deepin-linux","category-bloc-de-notes"],"_links":{"self":[{"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/19128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19128"}],"version-history":[{"count":4,"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/19128\/revisions"}],"predecessor-version":[{"id":19134,"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/19128\/revisions\/19134"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/media\/3167"}],"wp:attachment":[{"href":"https:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}