Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96055 > unrolled thread

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

Started bytropical.dude.net@gmail.com
First post2015-09-06 04:50 -0700
Last post2015-09-06 05:47 -0700
Articles 18 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Can anyone help me run python scripts with http.server? tropical.dude.net@gmail.com - 2015-09-06 04:50 -0700
    Re: Can anyone help me run python scripts with http.server? Laura Creighton <lac@openend.se> - 2015-09-06 14:15 +0200
      Re: Can anyone help me run python scripts with http.server? tropical.dude.net@gmail.com - 2015-09-06 05:35 -0700
      Re: Can anyone help me run python scripts with http.server? tropical.dude.net@gmail.com - 2015-09-06 05:38 -0700
        Re: Can anyone help me run python scripts with http.server? Laura Creighton <lac@openend.se> - 2015-09-06 14:45 +0200
          Re: Can anyone help me run python scripts with http.server? tropical.dude.net@gmail.com - 2015-09-06 06:07 -0700
            Re: Can anyone help me run python scripts with http.server? Chris Angelico <rosuav@gmail.com> - 2015-09-06 23:23 +1000
              Re: Can anyone help me run python scripts with http.server? Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-06 20:04 +0000
                Re: Can anyone help me run python scripts with http.server? Chris Angelico <rosuav@gmail.com> - 2015-09-07 12:48 +1000
            Re: Can anyone help me run python scripts with http.server? Laura Creighton <lac@openend.se> - 2015-09-06 17:03 +0200
            Re: Can anyone help me run python scripts with http.server? Chris Angelico <rosuav@gmail.com> - 2015-09-07 01:13 +1000
        Re: Can anyone help me run python scripts with http.server? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-06 13:11 -0400
      Re: Can anyone help me run python scripts with http.server? tropical.dude.net@gmail.com - 2015-09-06 05:42 -0700
    Re: Can anyone help me run python scripts with http.server? Chris Warrick <kwpolska@gmail.com> - 2015-09-06 14:16 +0200
    Re: Can anyone help me run python scripts with http.server? Laura Creighton <lac@openend.se> - 2015-09-06 14:28 +0200
    Re: Can anyone help me run python scripts with http.server? Peter Otten <__peter__@web.de> - 2015-09-06 14:32 +0200
    Re: Can anyone help me run python scripts with http.server? Peter Otten <__peter__@web.de> - 2015-09-06 14:43 +0200
    Re: Can anyone help me run python scripts with http.server? tropical.dude.net@gmail.com - 2015-09-06 05:47 -0700

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

Fromtropical.dude.net@gmail.com
Date2015-09-06 04:50 -0700
SubjectCan anyone help me run python scripts with http.server?
Message-ID<3877a9ae-edbd-4d36-925a-5cb3789bd1bd@googlegroups.com>
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.

[toc] | [next] | [standalone]


#96056

FromLaura Creighton <lac@openend.se>
Date2015-09-06 14:15 +0200
Message-ID<mailman.170.1441541749.8327.python-list@python.org>
In reply to#96055
In a message of Sun, 06 Sep 2015 04:50:23 -0700, tropical.dude.net@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

[toc] | [prev] | [next] | [standalone]


#96060

Fromtropical.dude.net@gmail.com
Date2015-09-06 05:35 -0700
Message-ID<cf53a05b-45c0-40c0-8d16-5c1bf3f1f7a3@googlegroups.com>
In reply to#96056
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@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

[toc] | [prev] | [next] | [standalone]


#96061

Fromtropical.dude.net@gmail.com
Date2015-09-06 05:38 -0700
Message-ID<31ec836b-4cb0-4079-8c12-f6b66a5cd143@googlegroups.com>
In reply to#96056
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@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

[toc] | [prev] | [next] | [standalone]


#96065

FromLaura Creighton <lac@openend.se>
Date2015-09-06 14:45 +0200
Message-ID<mailman.176.1441543545.8327.python-list@python.org>
In reply to#96061
In a message of Sun, 06 Sep 2015 05:38:50 -0700, tropical.dude.net@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

[toc] | [prev] | [next] | [standalone]


#96067

Fromtropical.dude.net@gmail.com
Date2015-09-06 06:07 -0700
Message-ID<361cf277-b469-4873-891d-0153498794ef@googlegroups.com>
In reply to#96065
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@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.

[toc] | [prev] | [next] | [standalone]


#96068

FromChris Angelico <rosuav@gmail.com>
Date2015-09-06 23:23 +1000
Message-ID<mailman.177.1441545797.8327.python-list@python.org>
In reply to#96067
On Sun, Sep 6, 2015 at 11:07 PM,  <tropical.dude.net@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#96082

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-09-06 20:04 +0000
Message-ID<msi68h$64n$1@dont-email.me>
In reply to#96068
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@gmail.com

[toc] | [prev] | [next] | [standalone]


#96092

FromChris Angelico <rosuav@gmail.com>
Date2015-09-07 12:48 +1000
Message-ID<mailman.191.1441594545.8327.python-list@python.org>
In reply to#96082
On Mon, Sep 7, 2015 at 6:04 AM, Denis McMahon <denismfmcmahon@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#96069

FromLaura Creighton <lac@openend.se>
Date2015-09-06 17:03 +0200
Message-ID<mailman.178.1441551833.8327.python-list@python.org>
In reply to#96067
In a message of Sun, 06 Sep 2015 06:07:05 -0700, tropical.dude.net@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

[toc] | [prev] | [next] | [standalone]


#96070

FromChris Angelico <rosuav@gmail.com>
Date2015-09-07 01:13 +1000
Message-ID<mailman.179.1441552424.8327.python-list@python.org>
In reply to#96067
On Mon, Sep 7, 2015 at 1:03 AM, Laura Creighton <lac@openend.se> 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

[toc] | [prev] | [next] | [standalone]


#96071

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-09-06 13:11 -0400
Message-ID<mailman.180.1441559501.8327.python-list@python.org>
In reply to#96061
On Sun, 6 Sep 2015 05:38:50 -0700 (PDT), tropical.dude.net@gmail.com
declaimed the following:

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

	And what does the Apache log(s) show... Presuming your URL reaches
Apache, the log should (I don't run a webserver) have some information of
what was requested, what Apache did with it, etc.

	If it is handling static pages, but not Python scripts, it could be
that the environment PATH variable that the server runs with does not have
the path to Python on it; or the environment does not have .py defined as
the extension of an executable program...

	Just repeating "it doesn't work" is not going to find a solution!

"It doesn't work" : you are sending requests to a port that isn't listening
for web requests

"It doesn't work" : the server doesn't know where to look for the page

"It doesn't work" : the server doesn't have privileges to run code

"It doesn't work" : the code is started but fails to return anything the
server can send to the user

...

	Log files should give some idea of which of those "it doesn't work"
might be the real problem.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#96062

Fromtropical.dude.net@gmail.com
Date2015-09-06 05:42 -0700
Message-ID<75f7fbea-ef68-48cc-b3b7-512e7febdd77@googlegroups.com>
In reply to#96056
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 

[toc] | [prev] | [next] | [standalone]


#96057

FromChris Warrick <kwpolska@gmail.com>
Date2015-09-06 14:16 +0200
Message-ID<mailman.171.1441541806.8327.python-list@python.org>
In reply to#96055
On 6 September 2015 at 13:50,  <tropical.dude.net@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.
> --
> 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

[toc] | [prev] | [next] | [standalone]


#96058

FromLaura Creighton <lac@openend.se>
Date2015-09-06 14:28 +0200
Message-ID<mailman.172.1441542515.8327.python-list@python.org>
In reply to#96055
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

[toc] | [prev] | [next] | [standalone]


#96059

FromPeter Otten <__peter__@web.de>
Date2015-09-06 14:32 +0200
Message-ID<mailman.173.1441542739.8327.python-list@python.org>
In reply to#96055
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 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
$ 

[toc] | [prev] | [next] | [standalone]


#96063

FromPeter Otten <__peter__@web.de>
Date2015-09-06 14:43 +0200
Message-ID<mailman.174.1441543420.8327.python-list@python.org>
In reply to#96055
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/<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
> $

[toc] | [prev] | [next] | [standalone]


#96066

Fromtropical.dude.net@gmail.com
Date2015-09-06 05:47 -0700
Message-ID<e723a220-8f95-4602-9d3f-e0cbe021c7e0@googlegroups.com>
In reply to#96055
On Sunday, September 6, 2015 at 1:51:00 PM UTC+2, tropical...@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web