Multiple level arguments, how would I do it.

381 views
Skip to first unread message

Beau

unread,
Mar 25, 2012, 11:51:20 PM3/25/12
to web.py
Hello,

I've been playing around with web.py and mimerender to create a little
webservice that will query other devices and respond via html/json/
xml.

What I want to do is have the url in the format of.

/identifier/function/arg1

or

/function/identifier/arg1

i've noticed get can return the url(?) via name. So this works great
for single arguments, but how would I extend this to multiple ones?.
Splitting on / seems like it would be a bad idea, and there is
probably a better way I'm overlooking.

Cameron Wengert

unread,
Mar 26, 2012, 2:24:18 PM3/26/12
to we...@googlegroups.com
You should just be able to handle this with a regex in the url definitions. 

urls = ('/(.*?)/(.*?)/(.*?)', 'handler')

class handler:

    def GET(self, identifier, function, arg):

I wouldn't be surprised if there is a better way to do this though...


--
You received this message because you are subscribed to the Google Groups "web.py" group.
To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.


Beau

unread,
Apr 3, 2012, 8:49:40 PM4/3/12
to web.py
That is exactly the solution :). Thanks.

罗晟

unread,
Apr 9, 2012, 12:02:34 AM4/9/12
to we...@googlegroups.com
Inspired me! Thanks.

But what about when the second and third parameters are not necessary?
Which means following urls need to be matched:
* /function
* /function/identifier
* /function/identifier/arg1

This puzzled me for quite a long time...

在 2012年3月27日星期二UTC+8上午2时24分18秒,PhantomXC写道:
To unsubscribe from this group, send email to webpy+unsubscribe@googlegroups.com.

NSC

unread,
Apr 9, 2012, 11:51:20 AM4/9/12
to web.py
I'm doing something similar, as I convert an asp.net application.

my url mapping:
urls = (
'/', 'index',
'/uiMethods/(.*)', 'uiMethods'
)

This way, anything that hits /uiMethods/foo is a web method call, and
I can still do normal mappings for static pages, templates, etc.

I then use the following handler for GET and POST, which in turn call
the function by *name*

class uiMethods:
def GET(self, method):
try:
methodToCall = getattr(self, method)
result = methodToCall()
return result
except Exception as ex:
raise ex

def POST(self, method):
try:
methodToCall = getattr(self, method)
result = methodToCall()
return result
except Exception as ex:
raise ex

def hello(self):
print "hello"

So, localhost:8080/uiMethods/hello will now map to your function, and
you can add new functions all day long without messing around with
more url mappings.

Cameron Wengert

unread,
Apr 9, 2012, 11:36:45 PM4/9/12
to we...@googlegroups.com
You could handle most of that with a regex as well. (I'm 99.7% positive this is wrong as I suck with regex)

  urls = '/(.*?)(?:/(.*?))(?:/(.*?))?', 'handler',

  def GET(self, function, identifier=None, arg=None):



To view this discussion on the web visit https://groups.google.com/d/msg/webpy/-/_p-FY9xChx8J.

To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages