CINXE.COM
Example webproxy.py - gevent 24.11.2.dev0 documentation
<!doctype html> <html class="no-js" lang="en" data-content_root="../"> <head><meta charset="utf-8"/> <meta name="viewport" content="width=device-width,initial-scale=1"/> <meta name="color-scheme" content="light dark"><meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="index" title="Index" href="../genindex.html" /><link rel="search" title="Search" href="../search.html" /><link rel="next" title="Example webpy.py" href="webpy.html" /><link rel="prev" title="Example unixsocket_server.py" href="unixsocket_server.html" /> <!-- Generated with Sphinx 8.1.3 and Furo 2024.08.06 --> <title>Example webproxy.py - gevent 24.11.2.dev0 documentation</title> <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a3258c64" /> <link rel="stylesheet" type="text/css" href="../_static/styles/furo.css?v=354aac6f" /> <link rel="stylesheet" type="text/css" href="../_static/styles/furo-extensions.css?v=302659d7" /> <link rel="stylesheet" type="text/css" href="../_static/custom.css?v=e405f49d" /> <style> body { --color-code-background: #eeeedd; --color-code-foreground: black; --color-brand-primary: #7c9a5e; --color-brand-content: #7c9a5e; --color-foreground-border: #b7d897; --font-stack: "SF Pro",-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"; --font-stack--monospace: "JetBrainsMono", "JetBrains Mono", "JetBrains Mono Regular", "JetBrainsMono-Regular", ui-monospace, profont, monospace; } @media not print { body[data-theme="dark"] { --color-code-background: #202020; --color-code-foreground: #d0d0d0; } @media (prefers-color-scheme: dark) { body:not([data-theme="light"]) { --color-code-background: #202020; --color-code-foreground: #d0d0d0; } } } </style><script async type="text/javascript" src="/_/static/javascript/readthedocs-addons.js"></script><meta name="readthedocs-project-slug" content="python-gevent" /><meta name="readthedocs-version-slug" content="latest" /><meta name="readthedocs-resolver-filename" content="/examples/webproxy.html" /><meta name="readthedocs-http-status" content="200" /></head> <body> <script> document.body.dataset.theme = localStorage.getItem("theme") || "auto"; </script> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="svg-toc" viewBox="0 0 24 24"> <title>Contents</title> <svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024"> <path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 0 0 0 13.8z"/> </svg> </symbol> <symbol id="svg-menu" viewBox="0 0 24 24"> <title>Menu</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-menu"> <line x1="3" y1="12" x2="21" y2="12"></line> <line x1="3" y1="6" x2="21" y2="6"></line> <line x1="3" y1="18" x2="21" y2="18"></line> </svg> </symbol> <symbol id="svg-arrow-right" viewBox="0 0 24 24"> <title>Expand</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-chevron-right"> <polyline points="9 18 15 12 9 6"></polyline> </svg> </symbol> <symbol id="svg-sun" viewBox="0 0 24 24"> <title>Light mode</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="feather-sun"> <circle cx="12" cy="12" r="5"></circle> <line x1="12" y1="1" x2="12" y2="3"></line> <line x1="12" y1="21" x2="12" y2="23"></line> <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line> <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line> <line x1="1" y1="12" x2="3" y2="12"></line> <line x1="21" y1="12" x2="23" y2="12"></line> <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line> <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line> </svg> </symbol> <symbol id="svg-moon" viewBox="0 0 24 24"> <title>Dark mode</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" /> </svg> </symbol> <symbol id="svg-sun-with-moon" viewBox="0 0 24 24"> <title>Auto light/dark, in light mode</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-custom-derived-from-feather-sun-and-tabler-moon"> <path style="opacity: 50%" d="M 5.411 14.504 C 5.471 14.504 5.532 14.504 5.591 14.504 C 3.639 16.319 4.383 19.569 6.931 20.352 C 7.693 20.586 8.512 20.551 9.25 20.252 C 8.023 23.207 4.056 23.725 2.11 21.184 C 0.166 18.642 1.702 14.949 4.874 14.536 C 5.051 14.512 5.231 14.5 5.411 14.5 L 5.411 14.504 Z"/> <line x1="14.5" y1="3.25" x2="14.5" y2="1.25"/> <line x1="14.5" y1="15.85" x2="14.5" y2="17.85"/> <line x1="10.044" y1="5.094" x2="8.63" y2="3.68"/> <line x1="19" y1="14.05" x2="20.414" y2="15.464"/> <line x1="8.2" y1="9.55" x2="6.2" y2="9.55"/> <line x1="20.8" y1="9.55" x2="22.8" y2="9.55"/> <line x1="10.044" y1="14.006" x2="8.63" y2="15.42"/> <line x1="19" y1="5.05" x2="20.414" y2="3.636"/> <circle cx="14.5" cy="9.55" r="3.6"/> </svg> </symbol> <symbol id="svg-moon-with-sun" viewBox="0 0 24 24"> <title>Auto light/dark, in dark mode</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-custom-derived-from-feather-sun-and-tabler-moon"> <path d="M 8.282 7.007 C 8.385 7.007 8.494 7.007 8.595 7.007 C 5.18 10.184 6.481 15.869 10.942 17.24 C 12.275 17.648 13.706 17.589 15 17.066 C 12.851 22.236 5.91 23.143 2.505 18.696 C -0.897 14.249 1.791 7.786 7.342 7.063 C 7.652 7.021 7.965 7 8.282 7 L 8.282 7.007 Z"/> <line style="opacity: 50%" x1="18" y1="3.705" x2="18" y2="2.5"/> <line style="opacity: 50%" x1="18" y1="11.295" x2="18" y2="12.5"/> <line style="opacity: 50%" x1="15.316" y1="4.816" x2="14.464" y2="3.964"/> <line style="opacity: 50%" x1="20.711" y1="10.212" x2="21.563" y2="11.063"/> <line style="opacity: 50%" x1="14.205" y1="7.5" x2="13.001" y2="7.5"/> <line style="opacity: 50%" x1="21.795" y1="7.5" x2="23" y2="7.5"/> <line style="opacity: 50%" x1="15.316" y1="10.184" x2="14.464" y2="11.036"/> <line style="opacity: 50%" x1="20.711" y1="4.789" x2="21.563" y2="3.937"/> <circle style="opacity: 50%" cx="18" cy="7.5" r="2.169"/> </svg> </symbol> <symbol id="svg-pencil" viewBox="0 0 24 24"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-pencil-code"> <path d="M4 20h4l10.5 -10.5a2.828 2.828 0 1 0 -4 -4l-10.5 10.5v4" /> <path d="M13.5 6.5l4 4" /> <path d="M20 21l2 -2l-2 -2" /> <path d="M17 17l-2 2l2 2" /> </svg> </symbol> <symbol id="svg-eye" viewBox="0 0 24 24"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-eye-code"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M10 12a2 2 0 1 0 4 0a2 2 0 0 0 -4 0" /> <path d="M11.11 17.958c-3.209 -.307 -5.91 -2.293 -8.11 -5.958c2.4 -4 5.4 -6 9 -6c3.6 0 6.6 2 9 6c-.21 .352 -.427 .688 -.647 1.008" /> <path d="M20 21l2 -2l-2 -2" /> <path d="M17 17l-2 2l2 2" /> </svg> </symbol> </svg> <input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation"> <input type="checkbox" class="sidebar-toggle" name="__toc" id="__toc"> <label class="overlay sidebar-overlay" for="__navigation"> <div class="visually-hidden">Hide navigation sidebar</div> </label> <label class="overlay toc-overlay" for="__toc"> <div class="visually-hidden">Hide table of contents sidebar</div> </label> <a class="skip-to-content muted-link" href="#furo-main-content">Skip to content</a> <div class="page"> <header class="mobile-header"> <div class="header-left"> <label class="nav-overlay-icon" for="__navigation"> <div class="visually-hidden">Toggle site navigation sidebar</div> <i class="icon"><svg><use href="#svg-menu"></use></svg></i> </label> </div> <div class="header-center"> <a href="../contents.html"><div class="brand">gevent 24.11.2.dev0 documentation</div></a> </div> <div class="header-right"> <div class="theme-toggle-container theme-toggle-header"> <button class="theme-toggle"> <div class="visually-hidden">Toggle Light / Dark / Auto color theme</div> <svg class="theme-icon-when-auto-light"><use href="#svg-sun-with-moon"></use></svg> <svg class="theme-icon-when-auto-dark"><use href="#svg-moon-with-sun"></use></svg> <svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg> <svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg> </button> </div> <label class="toc-overlay-icon toc-header-icon no-toc" for="__toc"> <div class="visually-hidden">Toggle table of contents sidebar</div> <i class="icon"><svg><use href="#svg-toc"></use></svg></i> </label> </div> </header> <aside class="sidebar-drawer"> <div class="sidebar-container"> <div class="sidebar-sticky"><a class="sidebar-brand" href="../contents.html"> <div class="sidebar-logo-container"> <img class="sidebar-logo" src="../_static/5564530.png" alt="Logo"/> </div> </a><form class="sidebar-search-container" method="get" action="../search.html" role="search"> <input class="sidebar-search" placeholder="Search" name="q" aria-label="Search"> <input type="hidden" name="check_keywords" value="yes"> <input type="hidden" name="area" value="default"> </form> <div id="searchbox"></div><div class="sidebar-scroll"><div class="sidebar-tree"> <ul> <li class="toctree-l1"><a class="reference internal" href="../install.html">Installation and Requirements</a></li> <li class="toctree-l1 has-children"><a class="reference internal" href="../changelog.html">Changelog</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle navigation of Changelog</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_5.html">Changes for 1.5</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_4.html">Changes for 1.4</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_3.html">Changes for 1.3</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_2.html">Changes for 1.2</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_1.html">Changes for 1.1</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_0.html">Changes for 1.0</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_pre.html">Changes before gevent 1.0</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="../intro.html">Introduction</a></li> <li class="toctree-l1"><a class="reference internal" href="../api/gevent.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent</span></code> – common functions</a></li> <li class="toctree-l1"><a class="reference internal" href="../api/gevent.greenlet.html">Greenlet Objects</a></li> <li class="toctree-l1"><a class="reference internal" href="../servers.html">Implementing servers</a></li> <li class="toctree-l1 has-children"><a class="reference internal" href="../dns.html">Name Resolution (DNS)</a><input class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" role="switch" type="checkbox"/><label for="toctree-checkbox-2"><div class="visually-hidden">Toggle navigation of Name Resolution (DNS)</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.resolver.thread.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.thread</span></code> – thread based hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.resolver.ares.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.ares</span></code> – c-ares based hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.resolver.dnspython.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.dnspython</span></code> – Pure Python hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.resolver.blocking.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.blocking</span></code> – Non-cooperative resolver</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="../monitoring.html">Monitoring and Debugging gevent Applications</a></li> <li class="toctree-l1"><a class="reference internal" href="../loop_impls.html">Event Loop Implementations: libuv and libev</a></li> <li class="toctree-l1"><a class="reference internal" href="../configuration.html">Configuring gevent</a></li> </ul> <ul class="current"> <li class="toctree-l1 current has-children"><a class="reference internal" href="../api/index.html">API reference</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" role="switch" type="checkbox"/><label for="toctree-checkbox-3"><div class="visually-hidden">Toggle navigation of API reference</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul class="current"> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent</span></code> – common functions</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.timeout.html">Cooperative Timeouts Using <code class="docutils literal notranslate"><span class="pre">gevent.Timeout</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.greenlet.html">Greenlet Objects</a></li> <li class="toctree-l2 has-children"><a class="reference internal" href="../api/gevent.socket.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.socket</span></code> – Cooperative low-level networking interface</a><input class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" role="switch" type="checkbox"/><label for="toctree-checkbox-4"><div class="visually-hidden">Toggle navigation of gevent.socket – Cooperative low-level networking interface</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../api/gevent._socket3.html">Python 3 interface</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.ssl.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.ssl</span></code> – Secure Sockets Layer (SSL/TLS) module</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.select.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.select</span></code> – Waiting for I/O completion</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.selectors.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.selectors</span></code> – High-level IO Multiplexing</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.event.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.event</span></code> – Notifications of multiple listeners</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.queue.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.queue</span></code> – Synchronized queues</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.local.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.local</span></code> – Greenlet-local objects</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.lock.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.lock</span></code> – Locking primitives</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.hub.html"><code class="docutils literal notranslate"><span class="pre">gevent.hub</span></code> - The Event Loop and the Hub</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.core.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.core</span></code> - (deprecated) event loop abstraction</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent</span></code> – common functions</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.backdoor.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.backdoor</span></code> – Interactive greenlet-based network console that can be used in any process</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.baseserver.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.baseserver</span></code> – Base class for implementing servers</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.builtins.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.builtins</span></code> – gevent friendly implementations of builtin functions</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.contextvars.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.contextvars</span></code> – Cooperative <code class="docutils literal notranslate"><span class="pre">contextvars</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.core.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.core</span></code> - (deprecated) event loop abstraction</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.event.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.event</span></code> – Notifications of multiple listeners</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.events.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.events</span></code> – Publish/subscribe event infrastructure</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.exceptions.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.exceptions</span></code> – Exceptions</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.fileobject.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.fileobject</span></code> – Wrappers to make file-like objects cooperative</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.hub.html"><code class="docutils literal notranslate"><span class="pre">gevent.hub</span></code> - The Event Loop and the Hub</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.local.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.local</span></code> – Greenlet-local objects</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.lock.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.lock</span></code> – Locking primitives</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.monkey.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.monkey</span></code> – Make the standard library cooperative</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.os.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.os</span></code> – Low-level operating system functions from <code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.pool.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.pool</span></code> – Managing greenlets in a group</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.pywsgi.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.pywsgi</span></code> – A pure-Python, gevent-friendly WSGI server</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.queue.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.queue</span></code> – Synchronized queues</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.resolver.ares.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.ares</span></code> – c-ares based hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.resolver.blocking.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.blocking</span></code> – Non-cooperative resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.resolver.dnspython.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.dnspython</span></code> – Pure Python hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.resolver.thread.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.thread</span></code> – thread based hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.select.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.select</span></code> – Waiting for I/O completion</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.selectors.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.selectors</span></code> – High-level IO Multiplexing</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.server.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.server</span></code> – TCP/SSL server</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.signal.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.signal</span></code> – Cooperative implementation of special cases of <code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a></li> <li class="toctree-l2 has-children"><a class="reference internal" href="../api/gevent.socket.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.socket</span></code> – Cooperative low-level networking interface</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" role="switch" type="checkbox"/><label for="toctree-checkbox-5"><div class="visually-hidden">Toggle navigation of gevent.socket – Cooperative low-level networking interface</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../api/gevent._socket3.html">Python 3 interface</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.ssl.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.ssl</span></code> – Secure Sockets Layer (SSL/TLS) module</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.subprocess.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.subprocess</span></code> – Cooperative <code class="docutils literal notranslate"><span class="pre">subprocess</span></code> module</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.thread.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.thread</span></code> – Implementation of the standard <code class="xref py py-mod docutils literal notranslate"><span class="pre">thread</span></code> module that spawns greenlets</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.threading.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.threading</span></code> – Implementation of the standard <code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> using greenlets</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.threadpool.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.threadpool</span></code> - A pool of native threads</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.time.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.time</span></code> – Makes <em>sleep</em> gevent aware</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.util.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.util</span></code> – Low-level utilities</a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.ares.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.ares</span></code> – Backwards compatibility alias for <code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.cares</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="../api/gevent.wsgi.html"><code class="docutils literal notranslate"><span class="pre">gevent.wsgi</span></code> – Historical note only; does not exist</a></li> <li class="toctree-l2 current has-children"><a class="reference internal" href="index.html">Examples</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" role="switch" type="checkbox"/><label for="toctree-checkbox-6"><div class="visually-hidden">Toggle navigation of Examples</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul class="current"> <li class="toctree-l3"><a class="reference internal" href="concurrent_download.html">Example concurrent_download.py</a></li> <li class="toctree-l3"><a class="reference internal" href="dns_mass_resolve.html">Example dns_mass_resolve.py</a></li> <li class="toctree-l3"><a class="reference internal" href="echoserver.html">Example echoserver.py</a></li> <li class="toctree-l3"><a class="reference internal" href="geventsendfile.html">Example geventsendfile.py</a></li> <li class="toctree-l3"><a class="reference internal" href="portforwarder.html">Example portforwarder.py</a></li> <li class="toctree-l3"><a class="reference internal" href="processes.html">Example processes.py</a></li> <li class="toctree-l3"><a class="reference internal" href="psycopg2_pool.html">Example psycopg2_pool.py</a></li> <li class="toctree-l3"><a class="reference internal" href="threadpool.html">Example threadpool.py</a></li> <li class="toctree-l3"><a class="reference internal" href="udp_client.html">Example udp_client.py</a></li> <li class="toctree-l3"><a class="reference internal" href="udp_server.html">Example udp_server.py</a></li> <li class="toctree-l3"><a class="reference internal" href="unixsocket_client.html">Example unixsocket_client.py</a></li> <li class="toctree-l3"><a class="reference internal" href="unixsocket_server.html">Example unixsocket_server.py</a></li> <li class="toctree-l3 current current-page"><a class="current reference internal" href="#">Example webproxy.py</a></li> <li class="toctree-l3"><a class="reference internal" href="webpy.html">Example webpy.py</a></li> <li class="toctree-l3"><a class="reference internal" href="wsgiserver.html">Example wsgiserver.py</a></li> <li class="toctree-l3"><a class="reference internal" href="wsgiserver_ssl.html">Example wsgiserver_ssl.py</a></li> </ul> </li> </ul> </li> </ul> <ul> <li class="toctree-l1 has-children"><a class="reference internal" href="../development/index.html">Development</a><input class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" role="switch" type="checkbox"/><label for="toctree-checkbox-7"><div class="visually-hidden">Toggle navigation of Development</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l2"><a class="reference internal" href="../development/getting_started.html">Getting Started</a></li> <li class="toctree-l2"><a class="reference internal" href="../development/installing_from_source.html">Installing From Source</a></li> <li class="toctree-l2"><a class="reference internal" href="../development/running_tests.html">Running Tests</a></li> <li class="toctree-l2"><a class="reference internal" href="../development/ci.html">Continuous integration</a></li> <li class="toctree-l2"><a class="reference internal" href="../development/release_process.html">Release Process</a></li> </ul> </li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../success.html">Success stories</a></li> <li class="toctree-l1"><a class="reference internal" href="../community.html">Community</a></li> <li class="toctree-l1 has-children"><a class="reference internal" href="../older_releases.html">Information About Older Releases</a><input class="toctree-checkbox" id="toctree-checkbox-8" name="toctree-checkbox-8" role="switch" type="checkbox"/><label for="toctree-checkbox-8"><div class="visually-hidden">Toggle navigation of Information About Older Releases</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_5.html">What’s new in gevent 1.5</a><input class="toctree-checkbox" id="toctree-checkbox-9" name="toctree-checkbox-9" role="switch" type="checkbox"/><label for="toctree-checkbox-9"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.5</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_5.html">Changes for 1.5</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_4.html">What’s new in gevent 1.4</a><input class="toctree-checkbox" id="toctree-checkbox-10" name="toctree-checkbox-10" role="switch" type="checkbox"/><label for="toctree-checkbox-10"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.4</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_4.html">Changes for 1.4</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_3.html">What’s new in gevent 1.3</a><input class="toctree-checkbox" id="toctree-checkbox-11" name="toctree-checkbox-11" role="switch" type="checkbox"/><label for="toctree-checkbox-11"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.3</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_3.html">Changes for 1.3</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_2.html">What’s new in gevent 1.2</a><input class="toctree-checkbox" id="toctree-checkbox-12" name="toctree-checkbox-12" role="switch" type="checkbox"/><label for="toctree-checkbox-12"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.2</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_2.html">Changes for 1.2</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_1.html">What’s new in gevent 1.1</a><input class="toctree-checkbox" id="toctree-checkbox-13" name="toctree-checkbox-13" role="switch" type="checkbox"/><label for="toctree-checkbox-13"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.1</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_1.html">Changes for 1.1</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_0.html">What’s new in gevent 1.0</a><input class="toctree-checkbox" id="toctree-checkbox-14" name="toctree-checkbox-14" role="switch" type="checkbox"/><label for="toctree-checkbox-14"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.0</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_0.html">Changes for 1.0</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_5.html">Changes for 1.5</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_4.html">Changes for 1.4</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_3.html">Changes for 1.3</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_2.html">Changes for 1.2</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_1.html">Changes for 1.1</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_0.html">Changes for 1.0</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_pre.html">Changes before gevent 1.0</a></li> </ul> </li> </ul> </div> </div> </div> </div> </aside> <div class="main"> <div class="content"> <div class="article-container"> <a href="#" class="back-to-top muted-link"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> <path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12z"></path> </svg> <span>Back to top</span> </a> <div class="content-icon-container"> <div class="theme-toggle-container theme-toggle-content"> <button class="theme-toggle"> <div class="visually-hidden">Toggle Light / Dark / Auto color theme</div> <svg class="theme-icon-when-auto-light"><use href="#svg-sun-with-moon"></use></svg> <svg class="theme-icon-when-auto-dark"><use href="#svg-moon-with-sun"></use></svg> <svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg> <svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg> </button> </div> <label class="toc-overlay-icon toc-content-icon no-toc" for="__toc"> <div class="visually-hidden">Toggle table of contents sidebar</div> <i class="icon"><svg><use href="#svg-toc"></use></svg></i> </label> </div> <article role="main" id="furo-main-content"> <section id="example-webproxy-py"> <h1>Example webproxy.py<a class="headerlink" href="#example-webproxy-py" title="Link to this heading">¶</a></h1> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="ch">#!/usr/bin/env python</span> <span class="linenos"> 2</span><span class="sd">"""A web application that retrieves other websites for you.</span> <span class="linenos"> 3</span> <span class="linenos"> 4</span><span class="sd">To start serving the application on port 8088, type</span> <span class="linenos"> 5</span> <span class="linenos"> 6</span><span class="sd"> python webproxy.py</span> <span class="linenos"> 7</span> <span class="linenos"> 8</span><span class="sd">To start the server on some other interface/port, use</span> <span class="linenos"> 9</span> <span class="linenos"> 10</span><span class="sd"> python -m gevent.wsgi -p 8000 -i 0.0.0.0 webproxy.py</span> <span class="linenos"> 11</span> <span class="linenos"> 12</span><span class="sd">"""</span> <span class="linenos"> 13</span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span> <span class="linenos"> 14</span><span class="kn">from</span> <span class="nn">gevent</span> <span class="kn">import</span> <span class="n">monkey</span><span class="p">;</span> <span class="n">monkey</span><span class="o">.</span><span class="n">patch_all</span><span class="p">()</span> <span class="linenos"> 15</span><span class="kn">import</span> <span class="nn">sys</span> <span class="linenos"> 16</span><span class="kn">import</span> <span class="nn">re</span> <span class="linenos"> 17</span><span class="kn">import</span> <span class="nn">traceback</span> <span class="linenos"> 18</span> <span class="linenos"> 19</span><span class="k">try</span><span class="p">:</span> <span class="linenos"> 20</span> <span class="kn">from</span> <span class="nn">cgi</span> <span class="kn">import</span> <span class="n">escape</span> <span class="linenos"> 21</span><span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span> <span class="linenos"> 22</span> <span class="c1"># Python 3.8 removed this API</span> <span class="linenos"> 23</span> <span class="kn">from</span> <span class="nn">html</span> <span class="kn">import</span> <span class="n">escape</span> <span class="linenos"> 24</span> <span class="linenos"> 25</span><span class="k">try</span><span class="p">:</span> <span class="linenos"> 26</span> <span class="kn">import</span> <span class="nn">urllib2</span> <span class="linenos"> 27</span> <span class="kn">from</span> <span class="nn">urlparse</span> <span class="kn">import</span> <span class="n">urlparse</span> <span class="linenos"> 28</span> <span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">unquote</span> <span class="linenos"> 29</span><span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span> <span class="linenos"> 30</span> <span class="c1"># pylint:disable=import-error,no-name-in-module</span> <span class="linenos"> 31</span> <span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">request</span> <span class="k">as</span> <span class="n">urllib2</span> <span class="linenos"> 32</span> <span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">urlparse</span> <span class="linenos"> 33</span> <span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">unquote</span> <span class="linenos"> 34</span> <span class="linenos"> 35</span><span class="n">LISTEN</span> <span class="o">=</span> <span class="p">(</span><span class="s1">'127.0.0.1'</span><span class="p">,</span> <span class="mi">8088</span><span class="p">)</span> <span class="linenos"> 36</span> <span class="linenos"> 37</span> <span class="linenos"> 38</span><span class="k">def</span> <span class="nf">_as_bytes</span><span class="p">(</span><span class="n">s</span><span class="p">):</span> <span class="linenos"> 39</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span> <span class="c1"># Py3</span> <span class="linenos"> 40</span> <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">'utf-8'</span><span class="p">)</span> <span class="linenos"> 41</span> <span class="k">return</span> <span class="n">s</span> <span class="linenos"> 42</span> <span class="linenos"> 43</span> <span class="linenos"> 44</span><span class="k">def</span> <span class="nf">_as_str</span><span class="p">(</span><span class="n">s</span><span class="p">):</span> <span class="linenos"> 45</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span> <span class="c1"># Py3</span> <span class="linenos"> 46</span> <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">'latin-1'</span><span class="p">)</span> <span class="linenos"> 47</span> <span class="k">return</span> <span class="n">s</span> <span class="linenos"> 48</span> <span class="linenos"> 49</span> <span class="linenos"> 50</span><span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">env</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span> <span class="linenos"> 51</span> <span class="n">proxy_url</span> <span class="o">=</span> <span class="s1">'http://</span><span class="si">%s</span><span class="s1">/'</span> <span class="o">%</span> <span class="n">env</span><span class="p">[</span><span class="s1">'HTTP_HOST'</span><span class="p">]</span> <span class="linenos"> 52</span> <span class="n">method</span> <span class="o">=</span> <span class="n">env</span><span class="p">[</span><span class="s1">'REQUEST_METHOD'</span><span class="p">]</span> <span class="linenos"> 53</span> <span class="n">path</span> <span class="o">=</span> <span class="n">env</span><span class="p">[</span><span class="s1">'PATH_INFO'</span><span class="p">]</span> <span class="linenos"> 54</span> <span class="k">if</span> <span class="n">env</span><span class="p">[</span><span class="s1">'QUERY_STRING'</span><span class="p">]:</span> <span class="linenos"> 55</span> <span class="n">path</span> <span class="o">+=</span> <span class="s1">'?'</span> <span class="o">+</span> <span class="n">env</span><span class="p">[</span><span class="s1">'QUERY_STRING'</span><span class="p">]</span> <span class="linenos"> 56</span> <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s1">'/'</span><span class="p">)</span> <span class="linenos"> 57</span> <span class="linenos"> 58</span> <span class="k">if</span> <span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span> <span class="s1">''</span><span class="p">):</span> <span class="linenos"> 59</span> <span class="n">start_response</span><span class="p">(</span><span class="s1">'200 OK'</span><span class="p">,</span> <span class="p">[(</span><span class="s1">'Content-Type'</span><span class="p">,</span> <span class="s1">'text/html'</span><span class="p">)])</span> <span class="linenos"> 60</span> <span class="k">return</span> <span class="p">[</span><span class="n">FORM</span><span class="p">]</span> <span class="linenos"> 61</span> <span class="linenos"> 62</span> <span class="k">if</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">'GET'</span><span class="p">:</span> <span class="linenos"> 63</span> <span class="k">return</span> <span class="n">proxy</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">start_response</span><span class="p">,</span> <span class="n">proxy_url</span><span class="p">)</span> <span class="linenos"> 64</span> <span class="linenos"> 65</span> <span class="k">if</span> <span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="s1">'POST'</span><span class="p">,</span> <span class="s1">''</span><span class="p">):</span> <span class="linenos"> 66</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">env</span><span class="p">[</span><span class="s1">'wsgi.input'</span><span class="p">]</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sa">b</span><span class="s1">'='</span><span class="p">)</span> <span class="linenos"> 67</span> <span class="k">assert</span> <span class="n">key</span> <span class="o">==</span> <span class="sa">b</span><span class="s1">'url'</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="linenos"> 68</span> <span class="n">value</span> <span class="o">=</span> <span class="n">_as_str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="linenos"> 69</span> <span class="n">start_response</span><span class="p">(</span><span class="s1">'302 Found'</span><span class="p">,</span> <span class="p">[(</span><span class="s1">'Location'</span><span class="p">,</span> <span class="n">_as_str</span><span class="p">(</span><span class="n">join</span><span class="p">(</span><span class="n">proxy_url</span><span class="p">,</span> <span class="n">unquote</span><span class="p">(</span><span class="n">value</span><span class="p">))))])</span> <span class="linenos"> 70</span> <span class="k">elif</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">'POST'</span><span class="p">:</span> <span class="linenos"> 71</span> <span class="n">start_response</span><span class="p">(</span><span class="s1">'404 Not Found'</span><span class="p">,</span> <span class="p">[])</span> <span class="linenos"> 72</span> <span class="k">else</span><span class="p">:</span> <span class="linenos"> 73</span> <span class="n">start_response</span><span class="p">(</span><span class="s1">'501 Not Implemented'</span><span class="p">,</span> <span class="p">[])</span> <span class="linenos"> 74</span> <span class="k">return</span> <span class="p">[]</span> <span class="linenos"> 75</span> <span class="linenos"> 76</span> <span class="linenos"> 77</span><span class="k">def</span> <span class="nf">proxy</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">start_response</span><span class="p">,</span> <span class="n">proxy_url</span><span class="p">):</span> <span class="linenos"> 78</span> <span class="c1"># pylint:disable=too-many-locals</span> <span class="linenos"> 79</span> <span class="k">if</span> <span class="s1">'://'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">path</span><span class="p">:</span> <span class="linenos"> 80</span> <span class="n">path</span> <span class="o">=</span> <span class="s1">'http://'</span> <span class="o">+</span> <span class="n">path</span> <span class="linenos"> 81</span> <span class="linenos"> 82</span> <span class="k">try</span><span class="p">:</span> <span class="linenos"> 83</span> <span class="k">try</span><span class="p">:</span> <span class="linenos"> 84</span> <span class="n">response</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="linenos"> 85</span> <span class="k">except</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">HTTPError</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span> <span class="linenos"> 86</span> <span class="n">response</span> <span class="o">=</span> <span class="n">ex</span> <span class="linenos"> 87</span> <span class="nb">print</span><span class="p">(</span><span class="s1">'</span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">msg</span><span class="p">))</span> <span class="linenos"> 88</span> <span class="c1"># Beginning in Python 3.8, headers aren't guaranteed to arrive in</span> <span class="linenos"> 89</span> <span class="c1"># lowercase; we must do so ourself.</span> <span class="linenos"> 90</span> <span class="n">headers</span> <span class="o">=</span> <span class="p">[(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">response</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">DROP_HEADERS</span><span class="p">]</span> <span class="linenos"> 91</span> <span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">_params</span><span class="p">,</span> <span class="n">_query</span><span class="p">,</span> <span class="n">_fragment</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="linenos"> 92</span> <span class="n">host</span> <span class="o">=</span> <span class="p">(</span><span class="n">scheme</span> <span class="ow">or</span> <span class="s1">'http'</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'://'</span> <span class="o">+</span> <span class="n">netloc</span> <span class="linenos"> 93</span> <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span> <span class="c1"># pylint:disable=broad-except</span> <span class="linenos"> 94</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'error while reading </span><span class="si">%s</span><span class="s1">:</span><span class="se">\n</span><span class="s1">'</span> <span class="o">%</span> <span class="n">path</span><span class="p">)</span> <span class="linenos"> 95</span> <span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">()</span> <span class="linenos"> 96</span> <span class="n">tb</span> <span class="o">=</span> <span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">()</span> <span class="linenos"> 97</span> <span class="n">start_response</span><span class="p">(</span><span class="s1">'502 Bad Gateway'</span><span class="p">,</span> <span class="p">[(</span><span class="s1">'Content-Type'</span><span class="p">,</span> <span class="s1">'text/html'</span><span class="p">)])</span> <span class="linenos"> 98</span> <span class="c1"># pylint:disable=deprecated-method</span> <span class="linenos"> 99</span> <span class="n">error_str</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">ex</span><span class="p">)</span> <span class="ow">or</span> <span class="n">ex</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">or</span> <span class="s1">'Error'</span><span class="p">)</span> <span class="linenos">100</span> <span class="n">error_str</span> <span class="o">=</span> <span class="s1">'<h1></span><span class="si">%s</span><span class="s1"></h1><h2></span><span class="si">%s</span><span class="s1"></h2><pre></span><span class="si">%s</span><span class="s1"></pre>'</span> <span class="o">%</span> <span class="p">(</span><span class="n">error_str</span><span class="p">,</span> <span class="n">escape</span><span class="p">(</span><span class="n">path</span><span class="p">),</span> <span class="n">escape</span><span class="p">(</span><span class="n">tb</span><span class="p">))</span> <span class="linenos">101</span> <span class="k">return</span> <span class="p">[</span><span class="n">_as_bytes</span><span class="p">(</span><span class="n">error_str</span><span class="p">)]</span> <span class="linenos">102</span> <span class="k">else</span><span class="p">:</span> <span class="linenos">103</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Returning"</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span> <span class="linenos">104</span> <span class="n">start_response</span><span class="p">(</span><span class="s1">'</span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">msg</span><span class="p">),</span> <span class="n">headers</span><span class="p">)</span> <span class="linenos">105</span> <span class="n">data</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="linenos">106</span> <span class="n">data</span> <span class="o">=</span> <span class="n">fix_links</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">proxy_url</span><span class="p">,</span> <span class="n">host</span><span class="p">)</span> <span class="linenos">107</span> <span class="k">return</span> <span class="p">[</span><span class="n">data</span><span class="p">]</span> <span class="linenos">108</span> <span class="linenos">109</span> <span class="linenos">110</span><span class="k">def</span> <span class="nf">join</span><span class="p">(</span><span class="n">url1</span><span class="p">,</span> <span class="o">*</span><span class="n">rest</span><span class="p">):</span> <span class="linenos">111</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">rest</span><span class="p">:</span> <span class="linenos">112</span> <span class="k">return</span> <span class="n">url1</span> <span class="linenos">113</span> <span class="n">url2</span><span class="p">,</span> <span class="n">rest</span> <span class="o">=</span> <span class="n">rest</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">rest</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="linenos">114</span> <span class="n">url1</span> <span class="o">=</span> <span class="n">_as_bytes</span><span class="p">(</span><span class="n">url1</span><span class="p">)</span> <span class="linenos">115</span> <span class="n">url2</span> <span class="o">=</span> <span class="n">_as_bytes</span><span class="p">(</span><span class="n">url2</span><span class="p">)</span> <span class="linenos">116</span> <span class="k">if</span> <span class="n">url1</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="sa">b</span><span class="s1">'/'</span><span class="p">):</span> <span class="linenos">117</span> <span class="k">if</span> <span class="n">url2</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="sa">b</span><span class="s1">'/'</span><span class="p">):</span> <span class="linenos">118</span> <span class="k">return</span> <span class="n">join</span><span class="p">(</span><span class="n">url1</span> <span class="o">+</span> <span class="n">url2</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="o">*</span><span class="n">rest</span><span class="p">)</span> <span class="linenos">119</span> <span class="k">return</span> <span class="n">join</span><span class="p">(</span><span class="n">url1</span> <span class="o">+</span> <span class="n">url2</span><span class="p">,</span> <span class="o">*</span><span class="n">rest</span><span class="p">)</span> <span class="linenos">120</span> <span class="linenos">121</span> <span class="k">if</span> <span class="n">url2</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="sa">b</span><span class="s1">'/'</span><span class="p">):</span> <span class="linenos">122</span> <span class="k">return</span> <span class="n">join</span><span class="p">(</span><span class="n">url1</span> <span class="o">+</span> <span class="n">url2</span><span class="p">,</span> <span class="o">*</span><span class="n">rest</span><span class="p">)</span> <span class="linenos">123</span> <span class="linenos">124</span> <span class="k">return</span> <span class="n">join</span><span class="p">(</span><span class="n">url1</span> <span class="o">+</span> <span class="sa">b</span><span class="s1">'/'</span> <span class="o">+</span> <span class="n">url2</span><span class="p">,</span> <span class="o">*</span><span class="n">rest</span><span class="p">)</span> <span class="linenos">125</span> <span class="linenos">126</span> <span class="linenos">127</span><span class="k">def</span> <span class="nf">fix_links</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">proxy_url</span><span class="p">,</span> <span class="n">host_url</span><span class="p">):</span> <span class="linenos">128</span><span class="w"> </span><span class="sd">"""</span> <span class="linenos">129</span><span class="sd"> >>> fix_links("><img src=images/hp0.gif width=158", 'http://127.0.0.1:8088', 'www.google.com')</span> <span class="linenos">130</span><span class="sd"> '><img src="http://127.0.0.1:8088/www.google.com/images/hp0.gif" width=158'</span> <span class="linenos">131</span><span class="sd"> """</span> <span class="linenos">132</span> <span class="k">def</span> <span class="nf">fix_link_cb</span><span class="p">(</span><span class="n">m</span><span class="p">):</span> <span class="linenos">133</span> <span class="n">url</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s1">'url'</span><span class="p">)</span> <span class="linenos">134</span> <span class="k">if</span> <span class="sa">b</span><span class="s1">'://'</span> <span class="ow">in</span> <span class="n">url</span><span class="p">:</span> <span class="linenos">135</span> <span class="n">result</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s1">'before'</span><span class="p">)</span> <span class="o">+</span> <span class="sa">b</span><span class="s1">'"'</span> <span class="o">+</span> <span class="n">join</span><span class="p">(</span><span class="n">proxy_url</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span> <span class="o">+</span> <span class="sa">b</span><span class="s1">'"'</span> <span class="linenos">136</span> <span class="k">else</span><span class="p">:</span> <span class="linenos">137</span> <span class="n">result</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s1">'before'</span><span class="p">)</span> <span class="o">+</span> <span class="sa">b</span><span class="s1">'"'</span> <span class="o">+</span> <span class="n">join</span><span class="p">(</span><span class="n">proxy_url</span><span class="p">,</span> <span class="n">host_url</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span> <span class="o">+</span> <span class="sa">b</span><span class="s1">'"'</span> <span class="linenos">138</span> <span class="c1">#print('replaced %r -> %r' % (m.group(0), result))</span> <span class="linenos">139</span> <span class="k">return</span> <span class="n">result</span> <span class="linenos">140</span> <span class="n">data</span> <span class="o">=</span> <span class="n">_link_re_1</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">fix_link_cb</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="linenos">141</span> <span class="n">data</span> <span class="o">=</span> <span class="n">_link_re_2</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">fix_link_cb</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="linenos">142</span> <span class="k">return</span> <span class="n">data</span> <span class="linenos">143</span> <span class="linenos">144</span><span class="n">_link_re_1</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">br</span><span class="s1">'''(?P<before>(href|src|action)\s*=\s*)(?P<quote>['"])(?P<url>[^#].*?)(?P=quote)'''</span><span class="p">)</span> <span class="linenos">145</span><span class="n">_link_re_2</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">br</span><span class="s1">'''(?P<before>(href|src|action)\s*=\s*)(?P<url>[^'"#>][^ >]*)'''</span><span class="p">)</span> <span class="linenos">146</span> <span class="linenos">147</span><span class="c1"># The lowercase names of headers that we will *NOT* forward.</span> <span class="linenos">148</span><span class="n">DROP_HEADERS</span> <span class="o">=</span> <span class="p">{</span> <span class="linenos">149</span> <span class="s1">'transfer-encoding'</span><span class="p">,</span> <span class="linenos">150</span> <span class="s1">'set-cookie'</span> <span class="linenos">151</span><span class="p">}</span> <span class="linenos">152</span> <span class="linenos">153</span><span class="n">FORM</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">"""<html><head></span> <span class="linenos">154</span><span class="s2"><title>Web Proxy - gevent example</title></head><body></span> <span class="linenos">155</span><span class="s2"><table width=60</span><span class="si">% he</span><span class="s2">ight=100</span><span class="si">% a</span><span class="s2">lign=center></span> <span class="linenos">156</span><span class="s2"><tr height=30%><td align=center valign=bottom>Type in URL you want to visit and press Enter</td></tr></span> <span class="linenos">157</span><span class="s2"><tr><td align=center valign=top></span> <span class="linenos">158</span><span class="s2"><form action=/ method=post></span> <span class="linenos">159</span><span class="s2"><input size=80 name=url value="http://www.gevent.org"/></span> <span class="linenos">160</span><span class="s2"></form></span> <span class="linenos">161</span><span class="s2"></td></tr></span> <span class="linenos">162</span><span class="s2"></table></body></table></span> <span class="linenos">163</span><span class="s2">"""</span> <span class="linenos">164</span> <span class="linenos">165</span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span> <span class="linenos">166</span> <span class="kn">from</span> <span class="nn">gevent.pywsgi</span> <span class="kn">import</span> <span class="n">WSGIServer</span> <span class="linenos">167</span> <span class="nb">print</span><span class="p">(</span><span class="s1">'Serving on </span><span class="si">%s</span><span class="s1">...'</span> <span class="o">%</span> <span class="p">(</span><span class="n">LISTEN</span><span class="p">,))</span> <span class="linenos">168</span> <span class="n">WSGIServer</span><span class="p">(</span><span class="n">LISTEN</span><span class="p">,</span> <span class="n">application</span><span class="p">)</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span> </pre></div> </div> <p><a class="reference external" href="https://github.com/gevent/gevent/blob/master/examples/webproxy.py">Current source</a></p> </section> </article> </div> <footer> <div class="related-pages"> <a class="next-page" href="webpy.html"> <div class="page-info"> <div class="context"> <span>Next</span> </div> <div class="title">Example webpy.py</div> </div> <svg class="furo-related-icon"><use href="#svg-arrow-right"></use></svg> </a> <a class="prev-page" href="unixsocket_server.html"> <svg class="furo-related-icon"><use href="#svg-arrow-right"></use></svg> <div class="page-info"> <div class="context"> <span>Previous</span> </div> <div class="title">Example unixsocket_server.py</div> </div> </a> </div> <div class="bottom-of-page"> <div class="left-details"> <div class="copyright"> Copyright © 2009-2023, gevent contributors </div> Made with <a href="https://www.sphinx-doc.org/">Sphinx</a> and <a class="muted-link" href="https://pradyunsg.me">@pradyunsg</a>'s <a href="https://github.com/pradyunsg/furo">Furo</a> </div> <div class="right-details"> </div> </div> </footer> </div> <aside class="toc-drawer no-toc"> </aside> </div> </div><script src="../_static/documentation_options.js?v=32fdf4fc"></script> <script src="../_static/doctools.js?v=9bcbadda"></script> <script src="../_static/sphinx_highlight.js?v=dc90522c"></script> <script src="../_static/scripts/furo.js?v=5fa4622c"></script> </body> </html>