Created
February 1, 2026 16:57
-
-
Save MuhammetOzturk/ff7de6603cc7a0d76347e1b027d1db7f to your computer and use it in GitHub Desktop.
flask-test.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyPlnsyvuS+OdKEp0wLPIms8", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/MuhammetOzturk/ff7de6603cc7a0d76347e1b027d1db7f/flask-test.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "from google.colab import output\n", | |
| "import threading\n", | |
| "import http.server\n", | |
| "import socketserver\n", | |
| "\n", | |
| "PORT = 8001\n", | |
| "\n", | |
| "class Handler(http.server.BaseHTTPRequestHandler):\n", | |
| " def do_GET(self):\n", | |
| " html = b\"\"\"\n", | |
| " <!doctype html>\n", | |
| " <html>\n", | |
| " <head><meta charset=\"utf-8\"></head>\n", | |
| " <body>\n", | |
| " <h2>Colab Proxy Test</h2>\n", | |
| " <p>Eger bunu goruyorsan, server calisiyor.</p>\n", | |
| " </body>\n", | |
| " </html>\n", | |
| " \"\"\"\n", | |
| " self.send_response(200)\n", | |
| " self.send_header(\"Content-Type\", \"text/html; charset=utf-8\")\n", | |
| " self.send_header(\"Content-Length\", str(len(html)))\n", | |
| " self.end_headers()\n", | |
| " self.wfile.write(html)\n", | |
| "\n", | |
| " def log_message(self, format, *args):\n", | |
| " pass # log spam'i kapat\n", | |
| "\n", | |
| "class TCPServer(socketserver.TCPServer):\n", | |
| " allow_reuse_address = True\n", | |
| "\n", | |
| "def start_server():\n", | |
| " with TCPServer((\"0.0.0.0\", PORT), Handler) as httpd:\n", | |
| " print(f\"Server basladi: http://127.0.0.1:{PORT}\")\n", | |
| " httpd.serve_forever()\n", | |
| "\n", | |
| "threading.Thread(target=start_server, daemon=True).start()" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "972cc-TCBItF", | |
| "outputId": "a8e6f0d5-9258-4d48-cbce-5689b473dd1e" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Server basladi: http://127.0.0.1:8001\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "gfez4B_J2qav", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "e0656cee-7513-4148-b919-ddc291a00173" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Server çalışıyor, status: 200\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import requests\n", | |
| "\n", | |
| "\n", | |
| "try:\n", | |
| " r = requests.get(f\"http://127.0.0.1:{PORT}\", timeout=3)\n", | |
| " print(\"Server çalışıyor, status:\", r.status_code)\n", | |
| "except Exception as e:\n", | |
| " print(\"Server ÇALIŞMIYOR:\", e)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "output.serve_kernel_port_as_window(PORT)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 71 | |
| }, | |
| "id": "gLOHUOe65QlA", | |
| "outputId": "63da453c-9ceb-4c73-d7e9-e104b5a4861d" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "\u001b[31mWarning: This function may stop working due to changes in browser security.\n", | |
| "Try `serve_kernel_port_as_iframe` instead. \u001b[0m\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "text/plain": [ | |
| "<IPython.core.display.Javascript object>" | |
| ], | |
| "application/javascript": [ | |
| "(async (port, path, text, element) => {\n", | |
| " if (!google.colab.kernel.accessAllowed) {\n", | |
| " return;\n", | |
| " }\n", | |
| " element.appendChild(document.createTextNode(''));\n", | |
| " const url = await google.colab.kernel.proxyPort(port);\n", | |
| " const anchor = document.createElement('a');\n", | |
| " anchor.href = new URL(path, url).toString();\n", | |
| " anchor.target = '_blank';\n", | |
| " anchor.setAttribute('data-href', url + path);\n", | |
| " anchor.textContent = text;\n", | |
| " element.appendChild(anchor);\n", | |
| " })(8001, \"/\", \"https://localhost:8001/\", window.element)" | |
| ] | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "output.serve_kernel_port_as_iframe(PORT)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 421 | |
| }, | |
| "id": "4gaBxDrwCAjw", | |
| "outputId": "a4e2aa46-c7e9-4826-bebb-ff5efa21c491" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "text/plain": [ | |
| "<IPython.core.display.Javascript object>" | |
| ], | |
| "application/javascript": [ | |
| "(async (port, path, width, height, cache, element) => {\n", | |
| " if (!google.colab.kernel.accessAllowed && !cache) {\n", | |
| " return;\n", | |
| " }\n", | |
| " element.appendChild(document.createTextNode(''));\n", | |
| " const url = await google.colab.kernel.proxyPort(port, {cache});\n", | |
| " const iframe = document.createElement('iframe');\n", | |
| " iframe.src = new URL(path, url).toString();\n", | |
| " iframe.height = height;\n", | |
| " iframe.width = width;\n", | |
| " iframe.style.border = 0;\n", | |
| " iframe.allow = [\n", | |
| " 'accelerometer',\n", | |
| " 'autoplay',\n", | |
| " 'camera',\n", | |
| " 'clipboard-read',\n", | |
| " 'clipboard-write',\n", | |
| " 'gyroscope',\n", | |
| " 'magnetometer',\n", | |
| " 'microphone',\n", | |
| " 'serial',\n", | |
| " 'usb',\n", | |
| " 'xr-spatial-tracking',\n", | |
| " ].join('; ');\n", | |
| " element.appendChild(iframe);\n", | |
| " })(8001, \"/\", \"100%\", \"400\", false, window.element)" | |
| ] | |
| }, | |
| "metadata": {} | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "from google.colab.output import eval_js\n", | |
| "print(eval_js(f\"google.colab.kernel.proxyPort({PORT})\"))" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 35 | |
| }, | |
| "id": "lMmtfHvlBMY6", | |
| "outputId": "eeba33d7-d45a-4ce7-b768-9d98b9383c9f" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "https://8001-m-s-1blgjzki7ampr-d.us-east1-0.prod.colab.dev\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "2128937e" | |
| }, | |
| "source": [ | |
| "First, we set up a simple HTTP server in a Python thread. This server will respond with a basic HTML page when accessed." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "a8bd1774", | |
| "outputId": "422886c9-5849-4ee2-ef3f-137f7ed084e7" | |
| }, | |
| "source": [ | |
| "from google.colab import output\n", | |
| "import threading\n", | |
| "import http.server\n", | |
| "import socketserver\n", | |
| "\n", | |
| "PORT = 8001\n", | |
| "\n", | |
| "class Handler(http.server.BaseHTTPRequestHandler):\n", | |
| " def do_GET(self):\n", | |
| " html = b\"\"\"\n", | |
| " <!doctype html>\n", | |
| " <html>\n", | |
| " <head><meta charset=\"utf-8\"></head>\n", | |
| " <body>\n", | |
| " <h2>Colab Proxy Test</h2>\n", | |
| " <p>Eger bunu goruyorsan, server calisiyor.</p>\n", | |
| " </body>\n", | |
| " </html>\n", | |
| " \"\"\"\n", | |
| " self.send_response(200)\n", | |
| " self.send_header(\"Content-Type\", \"text/html; charset=utf-8\")\n", | |
| " self.send_header(\"Content-Length\", str(len(html)))\n", | |
| " self.end_headers()\n", | |
| " self.wfile.write(html)\n", | |
| "\n", | |
| " def log_message(self, format, *args):\n", | |
| " pass # log spam'i kapat\n", | |
| "\n", | |
| "class TCPServer(socketserver.TCPServer):\n", | |
| " allow_reuse_address = True\n", | |
| "\n", | |
| "def start_server():\n", | |
| " with TCPServer((\"0.0.0.0\", PORT), Handler) as httpd:\n", | |
| " print(f\"Server basladi: http://127.0.0.1:{PORT}\")\n", | |
| " httpd.serve_forever()\n", | |
| "\n", | |
| "threading.Thread(target=start_server, daemon=True).start()" | |
| ], | |
| "execution_count": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Server basladi: http://127.0.0.1:8001\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "dfa3c299" | |
| }, | |
| "source": [ | |
| "After the server is running, you can use `google.colab.kernel.proxyPort` to get a public URL that tunnels to your local server running on `PORT`. This URL can then be accessed from any web browser." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 35 | |
| }, | |
| "id": "756b3faa", | |
| "outputId": "ee151b52-a0c4-43e0-fa0f-ab505e793d0e" | |
| }, | |
| "source": [ | |
| "from google.colab.output import eval_js\n", | |
| "print(eval_js(f\"google.colab.kernel.proxyPort({PORT})\"))" | |
| ], | |
| "execution_count": 2, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "https://8001-m-s-nhfw77tnymy6-c.us-west4-0.prod.colab.dev\n" | |
| ] | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment