Path: csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'configure': 0.04; 'python3': 0.05; 'skip:p 60': 0.05; '(using': 0.07; 'skip:/ 10': 0.07; 'subject:help': 0.07; 'editor.': 0.09; 'mkdir': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'url:localhost': 0.09; 'python': 0.10; 'def': 0.13; 'apache': 0.14; 'subject:python': 0.14; 'server,': 0.15; '"get': 0.16; "(i'm": 0.16; 'chmod': 0.16; "chris'": 0.16; 'echo': 0.16; 'http.server': 0.16; 'name=name)': 0.16; 'out?': 0.16; 'quit.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:run': 0.16; 'url:8000': 0.16; 'url:py': 0.16; 'wrote:': 0.16; 'code,': 0.23; 'import': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; '[2]': 0.27; "skip:' 10": 0.28; 'cgi': 0.29; 'minimal': 0.30; 'anyone': 0.32; '[1]': 0.32; 'run': 0.33; 'http': 0.33; 'skip:/ 20': 0.33; 'server': 0.34; 'advice': 0.35; 'could': 0.35; 'but': 0.36; 'instead': 0.36; 'url:non-standard http port': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'starting': 0.37; 'listing': 0.38; 'green': 0.38; 'files': 0.38; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'hello,': 0.40; 'email addr:gmail.com': 0.62; 'show': 0.62; 'url:%20': 0.63; 'url:0': 0.63; 'world': 0.64; 'serving': 0.67; 'url:index': 0.67; 'guides': 0.72; 'click': 0.76; '127.0.0.1': 0.84; 'bottle': 0.84; 'curl': 0.84; 'otten': 0.84; 'skip:/ 30': 0.84; 'url:mountain': 0.84; 'browser).': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Can anyone help me run python scripts with http.server? Date: Sun, 06 Sep 2015 14:43:21 +0200 Organization: None References: <3877a9ae-edbd-4d36-925a-5cb3789bd1bd@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd88de.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441543420 news.xs4all.nl 23808 [2001:888:2000:d::a6]:49453 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5380 X-Received-Body-CRC: 3869606919 Xref: csiph.com comp.lang.python:96063 Peter Otten wrote: > tropical.dude.net@gmail.com wrote: > >> I want to use python for web development but I >> could not configure my Apache server to run python >> with the guides I found on the internet. >> >> Can anyone help me configure http.server >> to run python scripts? >> >> I ran the command python -m http.server --cgi to start the http server, >> and if I put index.html, I will see the page but if I use >> index.py, it doesn't show the page, I can only see the >> directory listing of the files and when I click on >> index.py, it doesn't run the code, I can see it just >> like in the editor. >> >> Can anyone help me out? > > While in the long run you're better off if you follow Chris' advice here's and also in the short run (using the already mentioned bottle): $ echo '#!/usr/bin/env python3 > import bottle > @bottle.route("/into/the/blue/") > def demo(name): > return bottle.template("Hello, {{name}}", name=name) > bottle.run()' > demo.py $ python3 demo.py & [2] 54321 $ Bottle v0.12.7 server starting up (using WSGIRefServer())... Listening on http://127.0.0.1:8080/ Hit Ctrl-C to quit. $ curl "http://localhost:8080/into/the/blue/Mountain" 127.0.0.1 - - [06/Sep/2015 14:41:44] "GET /into/the/blue/Mountain HTTP/1.1" 200 15 Hello, Mountain$ curl "http://localhost:8080/into/the/blue/Green%20Pastries" 127.0.0.1 - - [06/Sep/2015 14:41:47] "GET /into/the/blue/Green%20Pastries HTTP/1.1" 200 21 Hello, Green Pastries$ > a minimal cgi script run with http.server (I'm using curl instead of a > browser). > > $ mkdir cgi-bin > $ echo -e '#!/usr/bin/env > python3\nprint("Content-type:text/plain")\nprint()\nprint("Hello, world")' > > cgi-bin/index.py $ chmod u+x cgi-bin/index.py $ python3 -m http.server > --cgi & > [1] 12345 > $ Serving HTTP on 0.0.0.0 port 8000 ... > > $ curl http://localhost:8000/cgi-bin/index.py > 127.0.0.1 - - [06/Sep/2015 14:30:23] "GET /cgi-bin/index.py HTTP/1.1" 200 > - Hello, world > $