Discussion:
[Tutor] GET function not executed
Rahul Pathak
2015-10-18 05:09:15 UTC
Permalink
Hi,

I started with python web module and i have no experience with web.

I used the example which is on the web.py site -

----------------------------------
import web

urls = {
'/', 'index'
}

class index:
def GET(self):
return "Hello World"

if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
-----------------------------------------

But when I execute this code on terminal and put http://localhost:8080/ in
browser then I get "not found" in browser and at terminal I get error -

$ python webse.py
http://0.0.0.0:8080/
127.0.0.1:51204 - - [18/Oct/2015 10:29:46] "HTTP/1.1 GET /" - 404 Not Found
127.0.0.1:51204 - - [18/Oct/2015 10:29:47] "HTTP/1.1 GET /" - 404 Not Found
127.0.0.1:51204 - - [18/Oct/2015 10:29:47] "HTTP/1.1 GET /" - 404 Not Found

Looks like GET method is not visible and not getting executed.


Please help

Regards
Rahul
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-10-18 09:14:18 UTC
Permalink
Post by Rahul Pathak
Hi,
I started with python web module and i have no experience with web.
I used the example which is on the web.py site -
----------------------------------
import web
urls = {
'/', 'index'
}
return "Hello World"
app = web.application(urls, globals())
app.run()
-----------------------------------------
But when I execute this code on terminal and put http://localhost:8080/ in
browser then I get "not found" in browser and at terminal I get error -
$ python webse.py
http://0.0.0.0:8080/
127.0.0.1:51204 - - [18/Oct/2015 10:29:46] "HTTP/1.1 GET /" - 404 Not
Found 127.0.0.1:51204 - - [18/Oct/2015 10:29:47] "HTTP/1.1 GET /" - 404
Not Found 127.0.0.1:51204 - - [18/Oct/2015 10:29:47] "HTTP/1.1 GET /" -
404 Not Found
Looks like GET method is not visible and not getting executed.
The problem is
Post by Rahul Pathak
urls = {
'/', 'index'
}
The use of {...} makes this a set literal, and the order of the items in a
set is undefined. To prevent a class of attacks on web applications it may
even change between invocations:

$ for i in {1..10}; do echo -n "PYTHONHASHSEED=$i --> "; PYTHONHASHSEED=$i
python setdemo.py; done
PYTHONHASHSEED=1 --> set(['index', '/'])
PYTHONHASHSEED=2 --> set(['/', 'index'])
PYTHONHASHSEED=3 --> set(['index', '/'])
PYTHONHASHSEED=4 --> set(['index', '/'])
PYTHONHASHSEED=5 --> set(['/', 'index'])
PYTHONHASHSEED=6 --> set(['/', 'index'])
PYTHONHASHSEED=7 --> set(['index', '/'])
PYTHONHASHSEED=8 --> set(['/', 'index'])
PYTHONHASHSEED=9 --> set(['index', '/'])
PYTHONHASHSEED=10 --> set(['index', '/'])

If you do not set the PYTHONHASHSEED environment variable python picks one
at random. For every run of the script where "index" comes first web.py
would try to match the url provided by the browser against the regex
"^index$" instead of "^/$" and will of course fail.
The original code at

http://webpy.org/docs/0.3/tutorial

uses (...), i. e. a tuple instead of a set. Tuples and lists [...] preserve
the order as written, so your script should work once you change urls into a
tuple or list.

PS: Fortunately (!) the "wrong" order is quite frequent; otherwise you would
think that your program worked but would still see occasional hard to
reproduce glitches.


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-10-18 09:21:21 UTC
Permalink
Post by Peter Otten
The use of {...} makes this a set literal, and the order of the items in a
set is undefined. To prevent a class of attacks on web applications it may
Sorry, I forgot to include the source of setdemo.py. It contains just one
line:

print {'/', 'index'}
Post by Peter Otten
$ for i in {1..10}; do echo -n "PYTHONHASHSEED=$i --> "; PYTHONHASHSEED=$i
python setdemo.py; done
PYTHONHASHSEED=1 --> set(['index', '/'])
PYTHONHASHSEED=2 --> set(['/', 'index'])
PYTHONHASHSEED=3 --> set(['index', '/'])
PYTHONHASHSEED=4 --> set(['index', '/'])
PYTHONHASHSEED=5 --> set(['/', 'index'])
PYTHONHASHSEED=6 --> set(['/', 'index'])
PYTHONHASHSEED=7 --> set(['index', '/'])
PYTHONHASHSEED=8 --> set(['/', 'index'])
PYTHONHASHSEED=9 --> set(['index', '/'])
PYTHONHASHSEED=10 --> set(['index', '/'])
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-10-18 14:40:44 UTC
Permalink
Post by Rahul Pathak
Hi,
I started with python web module and i have no experience with web.
OK, web.py is not part of the standard library and I have no
experience with that specific framework. I also see Peter Otten
has picked up an issue about the urls set. But there's something
else that I notice which may be significant...
Post by Rahul Pathak
But when I execute this code on terminal and put http://localhost:8080/ in
browser then I get "not found" in browser and at terminal I get error -
$ python webse.py
http://0.0.0.0:8080/
127.0.0.1:51204 - - [18/Oct/2015 10:29:46] "HTTP/1.1 GET /" - 404 Not Found
Note the first line of output uses 0.0.0.0:8080 which may suggest
that's what the server is expecting?
But you are accessing localhost which is 127.0.0.1.

The fact that you even get some kind of error message suggests the
server is somehow detecting the localhost request but maybe its
looking in the wrong place for the GET result?

I don't know the framework and so don't know what the significance of
the 0.0.0.0 is but it might be relevant so I thought I'd point it out.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...