FAQ
Hello everyone,


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?


Thanks in advance.

Search Discussions

  • Laura Creighton at Sep 6, 2015 at 12:15 pm
    In a message of Sun, 06 Sep 2015 04:50:23 -0700, tropical.dude.net at gmail.com wr
    ites:
    Hello everyone,

    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?

    Thanks in advance.

    What operating system are you running? It sounds to me as if you haven't
    configured apache to add a Handler for python scripts, or you have, but
    forgot to restart apache after you did so.


    Laura
  • Chris Warrick at Sep 6, 2015 at 12:16 pm

    On 6 September 2015 at 13:50, wrote:
    Hello everyone,

    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?

    Thanks in advance.
    --
    https://mail.python.org/mailman/listinfo/python-list

    Don?t use http.server. Don?t use CGI. This is not how things work in Python.


    In Python, you should use a web framework to write your code. Web
    frameworks include Flask, Django, Pyramid? Find one that?s popular and
    that you like, and learn that. You won?t have any `.py` URLs, instead
    you will have modern pretty URLs with no extensions. And a lot of
    things will be abstracted ? no manual header creation, help with safe
    forms and databases.


    And then you will need to figure out how to run it. I personally use
    nginx and uwsgi for this, you may need to look for something else.


    Examples for Django:


    https://docs.djangoproject.com/en/1.8/#first-steps
    https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/


    --
    Chris Warrick <https://chriswarrick.com/>
    PGP: 5EAAEA16
  • Laura Creighton at Sep 6, 2015 at 12:28 pm

    In a message of Sun, 06 Sep 2015 14:16:37 +0200, Chris Warrick writes:
    Don?t use http.server. Don?t use CGI. This is not how things work in Python.

    In Python, you should use a web framework to write your code. Web
    frameworks include Flask, Django, Pyramid? Find one that?s popular and
    that you like, and learn that. You won?t have any `.py` URLs, instead
    you will have modern pretty URLs with no extensions. And a lot of
    things will be abstracted ? no manual header creation, help with safe
    forms and databases.

    This is really great advice, and if your idea is 'but I don't want
    anything as _big_ as a framework, look at flask http://flask.pocoo.org/
    and bottle http://bottlepy.org/docs/dev/index.html both of which
    are tiny microframeworks.


    Laura
  • Peter Otten at Sep 6, 2015 at 12:32 pm

    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 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
    $
  • Peter Otten at Sep 6, 2015 at 12:43 pm

    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
    $
  • Tropical Dude Net at Sep 6, 2015 at 12:35 pm

    On Sunday, September 6, 2015 at 2:16:04 PM UTC+2, Laura Creighton wrote:
    In a message of Sun, 06 Sep 2015 04:50:23 -0700, tropical.dude.net at gmail.com wr
    ites:
    Hello everyone,

    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?

    Thanks in advance.
    What operating system are you running? It sounds to me as if you haven't
    configured apache to add a Handler for python scripts, or you have, but
    forgot to restart apache after you did so.

    Laura

    I am using Windows 7, I followed the instructions, modified httpd.conf
    many times, restarted apache many times till I gave up
  • Tropical Dude Net at Sep 6, 2015 at 12:38 pm

    On Sunday, September 6, 2015 at 2:16:04 PM UTC+2, Laura Creighton wrote:
    In a message of Sun, 06 Sep 2015 04:50:23 -0700, tropical.dude.net at gmail.com wr
    ites:
    Hello everyone,

    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?

    Thanks in advance.
    What operating system are you running? It sounds to me as if you haven't
    configured apache to add a Handler for python scripts, or you have, but
    forgot to restart apache after you did so.

    Laura

    I am using Windows 7, I followed the instructions, modified httpd.conf
    many times, restarted apache many times till I gave up
  • Laura Creighton at Sep 6, 2015 at 12:45 pm
    In a message of Sun, 06 Sep 2015 05:38:50 -0700, tropical.dude.net at gmail.com wr
    ites:

    What operating system are you running? It sounds to me as if you haven't
    configured apache to add a Handler for python scripts, or you have, but
    forgot to restart apache after you did so.

    Laura
    I am using Windows 7, I followed the instructions, modified httpd.conf
    many times, restarted apache many times till I gave up

    I'm not a Windows user. You are going to have to get help with this
    from somebody else.


    Though I forgot to send you this list:
    https://wiki.python.org/moin/WebFrameworks/


    if you decide that investigating Python web frameworks is what you want to
    do.


    Laura
  • Tropical Dude Net at Sep 6, 2015 at 1:07 pm

    On Sunday, September 6, 2015 at 2:46:42 PM UTC+2, Laura Creighton wrote:
    In a message of Sun, 06 Sep 2015 05:38:50 -0700, tropical.dude.net at gmail.com wr
    ites:
    What operating system are you running? It sounds to me as if you haven't
    configured apache to add a Handler for python scripts, or you have, but
    forgot to restart apache after you did so.

    Laura
    I am using Windows 7, I followed the instructions, modified httpd.conf
    many times, restarted apache many times till I gave up
    I'm not a Windows user. You are going to have to get help with this
    from somebody else.

    Though I forgot to send you this list:
    https://wiki.python.org/moin/WebFrameworks/

    if you decide that investigating Python web frameworks is what you want to
    do.

    Laura

    Thank you very much.


    I will definitely look into python web frameworks in the future, they seem
    complicated to me compared to php for example. I am looking for the simplest
    way to test my python scripts till I understand better how it works and when
    I can configure my own web server to run the framework because I want develop my
    stuff offline.
  • Chris Angelico at Sep 6, 2015 at 1:23 pm

    On Sun, Sep 6, 2015 at 11:07 PM, wrote:
    I will definitely look into python web frameworks in the future, they seem
    complicated to me compared to php for example. I am looking for the simplest
    way to test my python scripts till I understand better how it works and when
    I can configure my own web server to run the framework because I want develop my
    stuff offline.

    Look into Flask. You can do something really simple:


    https://github.com/Rosuav/MinstrelHall


    All the code is in one file for simplicity, though for a larger
    project, you can break it up as required. (Compared to PHP, where
    you're _forced_ to have exactly one file per entry-point, this is a
    nice flexibility.) Then I just have one line in my Apache config file
    that looks something like this:


    WSGIScriptAlias / /path/to/scripts/MinstrelHall/mh.wsgi


    and Apache does all the rest. There's some cruft in that code to get
    around a few oddities, but for the most part, writing a page is as
    simple as writing a function, decorated to manage routing, that
    returns render_template("somefile.html"). It's pretty easy.


    ChrisA
  • Cameron Simpson at Sep 7, 2015 at 4:05 am

    On 06Sep2015 23:23, Chris Angelico wrote:
    On Sun, Sep 6, 2015 at 11:07 PM, wrote:
    I will definitely look into python web frameworks in the future, they seem
    complicated to me compared to php for example. I am looking for the simplest
    way to test my python scripts till I understand better how it works and when
    I can configure my own web server to run the framework because I want develop my
    stuff offline.
    Look into Flask. You can do something really simple:
    https://github.com/Rosuav/MinstrelHall

    All the code is in one file for simplicity, though for a larger
    project, you can break it up as required. (Compared to PHP, where
    you're _forced_ to have exactly one file per entry-point, this is a
    nice flexibility.) Then I just have one line in my Apache config file
    that looks something like this:

    WSGIScriptAlias / /path/to/scripts/MinstrelHall/mh.wsgi

    and Apache does all the rest. There's some cruft in that code to get
    around a few oddities, but for the most part, writing a page is as
    simple as writing a function, decorated to manage routing, that
    returns render_template("somefile.html"). It's pretty easy.

    Another nice thing about Flask is that you can run it standalone without
    Apache. I'm knocking something together right now using Flask, and I'm
    intending to run it without Apache at all. There'll be an haproxy in front of
    it for other reasons, but to get off the ground you don't even need that.


    Cheers,
    Cameron Simpson <cs@zip.com.au>


    If you take something apart and put it back together again enough times, you
    will eventually have enough parts left over to build a second one.
             - Ayse Sercan <ayse@netcom.com>
  • Chris Angelico at Sep 7, 2015 at 4:57 am

    On Mon, Sep 7, 2015 at 2:05 PM, Cameron Simpson wrote:
    Another nice thing about Flask is that you can run it standalone without
    Apache. I'm knocking something together right now using Flask, and I'm
    intending to run it without Apache at all. There'll be an haproxy in front
    of it for other reasons, but to get off the ground you don't even need that.

    Running a Flask app standalone is, if I'm not mistaken, good for
    low-traffic usage only. Makes it absolutely ideal for debugging, but
    not so great for production work. But since you don't have to change a
    single line of application code to slot it into Apache, and presumably
    likewise for working with haproxy or anything else, it's a worthwhile
    simplicity.


    I've taken a good number of students through a Python web programming
    course, and we use Flask all the way. Testing? "python run.py".
    Production? "git push heroku master". No effort required, no careful
    juggling of "this bit makes it work in production".


    ChrisA
  • Cameron Simpson at Sep 7, 2015 at 7:29 am

    On 07Sep2015 14:57, Chris Angelico wrote:
    On Mon, Sep 7, 2015 at 2:05 PM, Cameron Simpson wrote:
    Another nice thing about Flask is that you can run it standalone without
    Apache. I'm knocking something together right now using Flask, and I'm
    intending to run it without Apache at all. There'll be an haproxy in front
    of it for other reasons, but to get off the ground you don't even need that.
    Running a Flask app standalone is, if I'm not mistaken, good for
    low-traffic usage only.

    I know. But the current target use is low traffic:-)


    Cheers,
    Cameron Simpson <cs@zip.com.au>
  • Laura Creighton at Sep 6, 2015 at 3:03 pm
    In a message of Sun, 06 Sep 2015 06:07:05 -0700, tropical.dude.net at gmail.com wr
    ites:
    Thank you very much.

    I will definitely look into python web frameworks in the future, they seem
    complicated to me compared to php for example. I am looking for the simplest
    way to test my python scripts till I understand better how it works and when
    I can configure my own web server to run the framework because I want develop my
    stuff offline.

    I think that bottle or flask won't seem complicated to you ....


    As I was walking around town it occurred to me to ask if you have
    made your something.py file executable? I don't even know if
    apache on windows cares about such things, though if you are using cgi
    and a unix like system you will need to do this.


    Laura
  • Chris Angelico at Sep 6, 2015 at 3:13 pm

    On Mon, Sep 7, 2015 at 1:03 AM, Laura Creighton wrote:
    As I was walking around town it occurred to me to ask if you have
    made your something.py file executable? I don't even know if
    apache on windows cares about such things, though if you are using cgi
    and a unix like system you will need to do this.

    Windows doesn't have that concept, so it's not necessary.


    ChrisA
  • Denis McMahon at Sep 6, 2015 at 8:04 pm

    On Sun, 06 Sep 2015 23:23:14 +1000, Chris Angelico wrote:


    WSGIScriptAlias / /path/to/scripts/MinstrelHall/mh.wsgi

    One wonders if the OP has mod_wsgi installed.


    https://code.google.com/p/modwsgi/wiki/WhereToGetHelp might be useful too.


    --
    Denis McMahon, denismfmcmahon at gmail.com
  • Chris Angelico at Sep 7, 2015 at 2:48 am

    On Mon, Sep 7, 2015 at 6:04 AM, Denis McMahon wrote:
    On Sun, 06 Sep 2015 23:23:14 +1000, Chris Angelico wrote:

    WSGIScriptAlias / /path/to/scripts/MinstrelHall/mh.wsgi
    One wonders if the OP has mod_wsgi installed.

    https://code.google.com/p/modwsgi/wiki/WhereToGetHelp might be useful too.

    Yes, that is a consideration. But hopefully the OP can explore some
    possibilities and get to some more specific questions like "Apache is
    complaining that WSGIScriptAlias is an unknown directive - how do I
    correct this?", which are easily answered with a web search or a quick
    post to some forum (preferably an Apache-related one, as this isn't a
    Python question).


    ChrisA
  • Tropical Dude Net at Sep 6, 2015 at 12:42 pm

    On Sunday, September 6, 2015 at 2:16:04 PM UTC+2, Laura Creighton wrote:
    In a message of Sun, 06 Sep 2015 04:50:23 -0700, wr
    ites:
    Hello everyone,

    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?

    Thanks in advance.
    What operating system are you running? It sounds to me as if you haven't
    configured apache to add a Handler for python scripts, or you have, but
    forgot to restart apache after you did so.

    Laura

    I am using Windows 7, I followed the instructions, modified httpd.conf
    many times, restarted apache many times till I gave up
  • Tropical Dude Net at Sep 6, 2015 at 12:47 pm

    On Sunday, September 6, 2015 at 1:51:00 PM UTC+2, tropical... at gmail.com wrote:
    Hello everyone,

    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?

    Thanks in advance.

    Thanks for the replies, I know of django, I even saw some
    youtube videos about it but it seems very complicated to me.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 6, '15 at 11:50a
activeSep 7, '15 at 7:29a
posts20
users7
websitepython.org

People

Translate

site design / logo © 2019 Grokbase