routes.py and patterns with routes_app

278 views
Skip to first unread message

peter

unread,
Aug 29, 2012, 7:44:13 AM8/29/12
to web...@googlegroups.com
I will try this post again, unfortunately I did not have a copy

I tried this example routes.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
routes_app = ((r'/(?P<app>welcome|admin|app)\b.*', r'\g<app>'),
              (r'(.*)', r'myapp'),
              (r'/?(.*)', r'myapp'))

I restarted the server
web2py uses port 8002, on a windows 7 machine.

As I understand the example it should allow apps welcome admin and app, but for other apps it should revert to 'myapp'.

127.0.0.1:8002/welcome starts the welcome app as it should

127.0.0.1:8002/gallery starts the gallery app, so the routing does not appear to be working.
127.0.0.1:8002 starts the welcome app, so the routing does not appear to be working.

Does anyone have any ideas why it is not working. If I put a syntax error in then this gets flagged up, so it is being loaded.

I need to use pattern based routing within an app, so I need to get on top of this.

I would like, within routes_app to be able to route according to domain name.

So what would routes_app look like to route

127.0.0.1:8002 to welcome, and

localhost:8002 to admin

Thanks

Peter




Anthony

unread,
Aug 29, 2012, 7:53:02 AM8/29/12
to web...@googlegroups.com
routes_app is used to tell the routing system which applications have their own application specific routes.py (in the application folder rather than the web2py root folder). I think you want to use routes_in (probably with a matching routes_out) instead. See http://web2py.com/books/default/chapter/29/4#Pattern-based-system.

Anthony

peter

unread,
Aug 29, 2012, 9:46:05 AM8/29/12
to web...@googlegroups.com
Okay, I get it now, routes_app does not select the application, only where to get the substitute routes.py.  The book is right with hindsight, but did not lead me to the right understanding. Maybe it should be clearer that it only controls which routes.py is used. 

The phrase " This is enabled by configuring routes_app in the base routes.py to determine from an incoming URL the name of the application to be selected ". Does imply a bit more than this. Pattern based routing is confusing enough already.

Could you now explain the following.

If I have a URL


This selects my gallery app.

If in routes.py I put

routes_in = (('/gallery','/welcome'),)

It does now route

127.0.0.1:8002/gallery to the welcome app.

However if I change the routes_in to

routes_in = (('2/gallery','2/welcome'),)

it no longer routes

127.0.0.1:8002/gallery to the welcome app, but leaves it as the gallery app, so routes_in is no longer working.

Why is this?
Thanks


Jonathan Lundell

unread,
Aug 29, 2012, 9:59:22 AM8/29/12
to web...@googlegroups.com
On 29 Aug 2012, at 6:46 AM, peter <peterchu...@gmail.com> wrote:
Okay, I get it now, routes_app does not select the application, only where to get the substitute routes.py.  The book is right with hindsight, but did not lead me to the right understanding. Maybe it should be clearer that it only controls which routes.py is used. 

The phrase " This is enabled by configuring routes_app in the base routes.py to determine from an incoming URL the name of the application to be selected ". Does imply a bit more than this. Pattern based routing is confusing enough already.

Could you now explain the following.

If I have a URL


This selects my gallery app.

If in routes.py I put

routes_in = (('/gallery','/welcome'),)

It does now route

127.0.0.1:8002/gallery to the welcome app.

However if I change the routes_in to

routes_in = (('2/gallery','2/welcome'),)

it no longer routes

127.0.0.1:8002/gallery to the welcome app, but leaves it as the gallery app, so routes_in is no longer working.

Why is this?


If you're trying to match the 2 from the port number, that's not going to work. The incoming URL isn't presented as a single string, but is instead broken up into components. I don't have the syntax at hand, but if you review the docs with that in mind you'll probably figure it out. Or someone else will chime in with the right pattern.
Message has been deleted

peter

unread,
Aug 29, 2012, 11:13:54 AM8/29/12
to web...@googlegroups.com
I am not sure what docs you are referring to Jonathan. The book gives an example:

"The general syntax for routes is more complex than the simple examples we have seen so far. Here is a more general and representative example:

1.
2.
3.
4.
routes_in = (
(
'140\.191\.\d+\.\d+:https://www.web2py.com:POST /(?P<any>.*)\.php',
'/test/default/index?vars=\g<any>'),
)
"
that seems to imply the URL is a string. It would be useful if one could see the URL presented to routes_in.

Let me ask the second part of my initial post slightly revised.

I would like, within routes_In to be able to route according to domain name.

 

So what would routes_in look like to route

 127.0.0.1:8002 to welcome, and

 localhost:8002 to admin

Thanks
Peter

Jonathan Lundell

unread,
Aug 29, 2012, 11:21:00 AM8/29/12
to web...@googlegroups.com
On 29 Aug 2012, at 8:13 AM, peter <peterchu...@gmail.com> wrote:
I am not sure what docs you are referring to Jonathan. The book gives an example:

"The general syntax for routes is more complex than the simple examples we have seen so far. Here is a more general and representative example:

1.
2.
3.
4.
routes_in = (
(
'140\.191\.\d+\.\d+:https://www.web2py.com:POST /(?P<any>.*)\.php',
'/test/default/index?vars=\g<any>'),
)
"
that seems to imply the URL is a string. It would be useful if one could see the URL presented to routes_in.

A little below that is the general structure of the incoming pattern.

'[remote address]:[protocol]://[host]:[method] [path]'

So I think you'd want something like: '.*://localhost:.* [path]


Let me ask the second part of my initial post slightly revised.

I would like, within routes_In to be able to route according to domain name.

 

So what would routes_in look like to route

 127.0.0.1:8002 to welcome, and

 localhost:8002 to admin

In the long run, what are you really trying to accomplish with your routes? 


peter

unread,
Aug 29, 2012, 1:08:56 PM8/29/12
to web...@googlegroups.com
I tried

routes_in = ((r' .*://localhost:.*',r'/welcome'),)

and 
routes_in = ((r' .*localhost.*',r'/welcome'),)


neither match localhost urls so I am at a loss. I would like to know how to match localhost.

In the short run I want to do

 domains={
     'ukjazz.net':'british_jazz',
     'www.ukjazz.net':'british_jazz',
     'lindseymalin.com':'gallery',
     'www.lindseymalin.com':'gallery'
}

with pattern matching rewrites. This is because I want to have a pattern matching routes.py for british_jazz

I am sure other people would like to do the equivalent of domains with patterns

Thanks
Peter

On Wednesday, 29 August 2012 16:21:12 UTC+1, Jonathan Lundell wrote:
A little below that is the general structure of the incoming pattern.

'[remote address]:[protocol]://[host]:[method] [path]'

Massimo Di Pierro

unread,
Aug 29, 2012, 1:19:26 PM8/29/12
to web...@googlegroups.com
Are you trying to match the remote address as localhost? The problem is that it depends on what you browser puts in there. You can try:
routes_in = [('127\.0\.0\.1:http://.*?:(GET|POST) /$anything','/welcome')]

Jonathan Lundell

unread,
Aug 29, 2012, 1:40:07 PM8/29/12
to web...@googlegroups.com
On 29 Aug 2012, at 10:08 AM, peter <peterchu...@gmail.com> wrote:
> I tried
>
> routes_in = ((r' .*://localhost:.*',r'/welcome'),)
>
> and
> routes_in = ((r' .*localhost.*',r'/welcome'),)
>
>
> neither match localhost urls so I am at a loss. I would like to know how to match localhost.

If there's actually a space at the beginning of the first pattern, it won't match.

A good place to see a lot of routing examples is in the unit-test files. gluon/tests/test_routes.py IIRC.
> --
>
>
>


peter

unread,
Aug 29, 2012, 2:15:03 PM8/29/12
to web...@googlegroups.com
Thanks for the suggestion Massimo
It did not match with

localhost or 127.0.0.1

eg localhost:8002/gallery

Peter

peter

unread,
Aug 30, 2012, 4:59:25 AM8/30/12
to web...@googlegroups.com
I got inside gluon. The conversion is done within regex_uri

I have found that 

becomes
127.0.0.1:http://127.0.0.1:get /gallery

just before the conversion

becomes
127.0.0.1:http://localhost:get /gallery

So Massimo was almost right the problem was putiing the get and post in upper case

routes_in = [('127\.0\.0\.1:http://.*?:(get|post) /$anything','/welcome')]
works
The answer to my challenge to get URLs with localhost to route to the admin app and URLs with 127.0.0.1 to route to the  welcome app

routes_in = [('.*://localhost:(get|post) /$anything','/admin'),
                           ('.*://127\.0\.0\.1:(get|post) /$anything','/welcome')]

So presumably to map mydomain to myapp would be

routes_in = [('.*://mydomain:(get|post) /myapp/$anything','/myapp/$anything'),
                  ('.*://mydomain:(get|post) /$anything','/myapp/$anything')]

or if using routes_app

routes_app=[('.*://mydomain:(get|post) /$anything','myapp'),


Peter

Jonathan Lundell

unread,
Aug 30, 2012, 10:39:44 AM8/30/12
to web...@googlegroups.com
On 30 Aug 2012, at 1:59 AM, peter <peterchu...@gmail.com> wrote:
> I got inside gluon. The conversion is done within regex_uri
>
> I have found that
>
> http://127.0.0.1:8002/gallery
> becomes
> 127.0.0.1:http://127.0.0.1:get /gallery
>
> just before the conversion
>
> http://localhost:8002/gallery
> becomes
> 127.0.0.1:http://localhost:get /gallery
>
> So Massimo was almost right the problem was putiing the get and post in upper case
>
> routes_in = [('127\.0\.0\.1:http://.*?:(get|post) /$anything','/welcome')]
> works
> The answer to my challenge to get URLs with localhost to route to the admin app and URLs with 127.0.0.1 to route to the welcome app
>
> routes_in = [('.*://localhost:(get|post) /$anything','/admin'),
> ('.*://127\.0\.0\.1:(get|post) /$anything','/welcome')]
>
> So presumably to map mydomain to myapp would be
>
> routes_in = [('.*://mydomain:(get|post) /myapp/$anything','/myapp/$anything'),
> ('.*://mydomain:(get|post) /$anything','/myapp/$anything')]
>
> or if using routes_app
>
> routes_app=[('.*://mydomain:(get|post) /$anything','myapp'),
>

Unless you really want to restrict routing to get & post (excluding, say, head), why not match on \w* instead of (get|post)?

routes_in = [('.*://localhost:\w* /$anything','/admin/$anything'),
('.*://127\.0\.0\.1:\w* /$anything','/welcome/$anything')]

For future reference, you can turn on rewrite logging to see what's going on: the router will log the regex strings that it's using, and the result of the rewrite.

Notice that this routes_in causes a problem (I think) in welcome/appadmin when it generates URLs with URL('admin', ...). These will come in from 127.0.0.1, and be routed to welcome instead of admin. So:

routes_in = [('.*://localhost:\w* /$anything','/admin/$anything'),
('.*://127\.0\.0\.1:\w* /admin/$anything','/admin/$anything'),
('.*://127\.0\.0\.1:\w* /$anything','/welcome/$anything')]

peter

unread,
Aug 30, 2012, 2:09:55 PM8/30/12
to web...@googlegroups.com
Thanks Jonathan. I wish I had known about rewrite logging a couple of days ago. Could I suggest that mention is made of this in the section of the book on rewriting URLs. It could save people a lot of time.

Could I also suggest that 

routes_app=[('.*://mydomain:\w* /$anything','myapp'), 

is given in the book as an example of mapping a domain to an app. I do not think that anyone would get to the above from the docs without a lot of trial and error. 

Thanks
Peter
Reply all
Reply to author
Forward
0 new messages