[Tutor] GET function not executed
Peter Otten
__peter__ at web.de
Sun Oct 18 05:14:18 EDT 2015
Rahul Pathak wrote:
> 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.
The problem is
> 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.
More information about the Tutor
mailing list