Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python3': 0.05; 'tool,': 0.07; 'abstraction': 0.09; 'apache,': 0.09; 'here?': 0.09; 'level:': 0.09; 'port,': 0.09; 'subject:Python3': 0.09; 'subject:skip:f 10': 0.09; 'url:github': 0.09; 'cc:addr:python- list': 0.10; 'python': 0.11; 'appropriate': 0.14; 'do,': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'handling.': 0.16; 'http.server': 0.16; 'insights': 0.16; 'maintainable': 0.16; 'pulling': 0.16; 'subject:server': 0.16; 'url:py': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'library,': 0.18; 'runs': 0.18; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'trying': 0.22; 'controlled': 0.22; 'level,': 0.22; 'am,': 0.23; 'code,': 0.23; '2015': 0.23; 'bigger': 0.23; 'header:In-Reply- To:1': 0.24; 'mon,': 0.24; 'install': 0.25; 'example': 0.25; 'appreciated.': 0.27; 'raw': 0.27; 'separate': 0.27; 'message- id:@mail.gmail.com': 0.28; 'options': 0.28; "doesn't": 0.28; 'appear': 0.29; 'interface': 0.29; 'directly,': 0.29; 'loop,': 0.29; 'running.': 0.29; 'correct': 0.29; 'program,': 0.29; 'books': 0.30; 'lines': 0.30; 'server.': 0.30; 'hall': 0.31; 'code': 0.31; 'run': 0.32; 'system,': 0.32; 'framework': 0.32; 'topic': 0.32; 'url:python': 0.33; 'problem': 0.33; 'case,': 0.34; 'file': 0.34; 'server': 0.34; 'received:google.com': 0.34; 'could': 0.35; 'eric': 0.35; 'path': 0.35; 'strongly': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'url:org': 0.36; 'there': 0.36; 'cases': 0.36; 'url:library': 0.36; 'urls': 0.36; 'two': 0.37; 'operating': 0.37; 'subject:: ': 0.37; 'level': 0.37; "won't": 0.38; 'say': 0.38; 'does': 0.39; 'url:docs': 0.39; 'seem': 0.39; 'subject:-': 0.39; 'where': 0.40; 'called': 0.40; 'url:3': 0.60; "you'll": 0.61; 'simple': 0.61; 'show': 0.62; 'high': 0.62; 'more': 0.62; 'information': 0.62; 'offer': 0.65; 'url:6': 0.66; 'special': 0.72; 'url:http': 0.73; 'subject: & ': 0.73; 'browser.': 0.81; 'low': 0.83; 'chrisa': 0.84; 'listens': 0.84; 'pip': 0.84; 'url:master': 0.84; 'to:none': 0.90 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=+NUnPaZf4VTfIJPyPX1184enlmnb4S2o/rV8VE7npO8=; b=dh/6rz3A2FqG4pUjdX4D/GlI3Dj7ylSuUKErAc8dFCozi4/apzQtV9DwDu+E8+BlEj aQD8azZPMrbXae1tIDD9yFdw6rG0xvFAJUBLRwB7juRvCysPhum7+R00GDFCSa71zSLp 5LlZ4GGC6O7fDWiSFTq0O3TAAqxosxEMndJ2HvOj0a3BMpOLaGQHsl77uv7u8/+JW72l aeSn2lCnYbyiHQ1FJRg7dzrGouOS/OTaPf0G4nrTgHAKj7VX/771SYXdBe5MVQBQmVUt fCyv/CP8XKqDFa2lS2DzZAsr1syy6m1Gv8y/BcWp9w8cy/eV5geByo0mkpZQ2q+9SDoC H/TA== MIME-Version: 1.0 X-Received: by 10.107.160.141 with SMTP id j135mr17149751ioe.43.1433722417594; Sun, 07 Jun 2015 17:13:37 -0700 (PDT) In-Reply-To: <4f0edbe7-c634-4c26-8e36-0f32c4238447@googlegroups.com> References: <4f0edbe7-c634-4c26-8e36-0f32c4238447@googlegroups.com> Date: Mon, 8 Jun 2015 10:13:37 +1000 Subject: Re: Built-in Python3 web server functionality - wsgiref & http.server From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433722428 news.xs4all.nl 2855 [2001:888:2000:d::a6]:51355 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92315 On Mon, Jun 8, 2015 at 5:13 AM, Eric wrote: > I am trying to better understand the built-in python3 web server function= ality and have just started reading the documentation. > > There seem to be two options provided by Python 3. > > 1. > wsgiref > https://docs.python.org/3/library/wsgiref.html > > 2. > http.server > https://docs.python.org/3/library/http.server.html > > > Both options appear to offer a simple way to get a simple web server up a= nd running. It does appear that wsgiref is built on top of http.server. Is = this correct? > > Would it be correct to say that wsgiref is a high(er?)-level framework fo= r designing web-based applications where as http.server is operating at a l= ower level? While one could do everything with just http.server, if wsgiref= was appropriate for a particular use case, it would be the path to follow. > > Any insights into this topic would be appreciated. > > Are there any good python 3 books covering this functionality? What are you actually trying to accomplish here? At what level of abstraction do you want to work? http.server is pretty low level: https://github.com/Rosuav/Yosemite/blob/master/Yosemite.py But if you want a higher level, I would recommend using something like Flas= k: https://github.com/Rosuav/MinstrelHall/blob/master/mh.py Yosemite.py runs as a stand-alone program, listens on a port, and can be controlled by a web browser. It's a web server. But if you look through the code, you'll see how raw an interface it is; that's not a big problem for Yosemite, as it's an extremely thin layer over a file system, so it's basically pulling out a few special cases ("/stop" etc) and then looking for something at the exact path it was given. Minstrel Hall runs inside Apache, via Passenger. You can also run it directly, in which case it makes use of an internal bind/listen/accept/dispatch loop, so it can also be called a web server. But it's much more clearly operating as a web site, with separate top-level functions that clearly show what URLs they're handling. There's support for parameterized URLs like http://minstrelhall.com/campaign/6 (comes through to campaign(id=3D6)), and heaps of other features that a simple example like MH won't show off. If you want to make a web site that's still maintainable after it's grown bigger than the ~300 lines of code that Yosemite.py has, I strongly recommend using a framework like Flask. On the flip side, if you want a quick-and-dirty program that shows you a whole lot of extremely raw information about what the web browser is trying to do, http.server is a very handy tool, and it doesn't require any extra installations (Flask isn't part of the standard library, so you'd need to pip install it). High or low abstraction level? ChrisA