Can anyone help me run python scripts with http.server?

Peter Otten __peter__ at web.de
Sun Sep 6 08:43:21 EDT 2015


Peter Otten wrote:

> tropical.dude.net at 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/<name>")
> 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
> $





More information about the Python-list mailing list