url mapping issue: No module named xxx

30 views
Skip to first unread message

Zhang Huangbin

unread,
Nov 15, 2009, 10:44:31 AM11/15/09
to web.py
Hi, all.

I got a strange url mapping issue in my app, but i don't know why it
happened. Please help me to find the root case. Thanks :)

Error msg:
----
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/web/application.py", line
241, in process
return self.handle()
File "/usr/lib/python2.4/site-packages/web/application.py", line
232, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/lib/python2.4/site-packages/web/application.py", line
407, in _delegate
mod = __import__(mod, globals(), locals(), [""])
ImportError: No module named /antispam/blacklist/helo/page/
controllers.policyd.wblist
----

URL mapping:
----
re_ip = r"""\b25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.25[0-5]|2[0-4]
[0-9]|[01]?[0-9][0-9]?|%\.25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|%\.25
[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|%$"""
re_sender = r'[\w\-][\w\-\.]*@[\w\-][\w\-\.]+[a-zA-Z]{1,4}'
re_dnsname = r'[\w\-][\w\-\.]+[a-zA-Z]{1,4}'

re_ip_f = r"""\b\d{1,3}|%$"""

urls = (
'/antispam',
'controllers.policyd.wblist.Whitelist',

#
# @/page/0 List ALL records without offset and limit in SQL.
#

# Whitelist: IP addresses.
'/antispam/(whitelist)',
'controllers.policyd.wblist.Whitelist',
'/antispam/(whitelist)/(ip)',
'controllers.policyd.wblist.Whitelist',
'/antispam/(whitelist)/(ip)/record/(%s)/(%s)/(%s)/(%s)' % (
re_ip_f, re_ip_f, re_ip_f, re_ip_f, ),
'controllers.policyd.wblist.WhitelistView',
'/antispam/(whitelist)/(ip)/page/([01-9]\d*)',
'controllers.policyd.wblist.Whitelist',

# Whitelist: Sender addresses.
'/antispam/(whitelist)/(sender)',
'controllers.policyd.wblist.Whitelist',
'/antispam/(whitelist)/(sender)/%s' % re_ip,
'controllers.policyd.wblist.Whitelist',
'/antispam/(whitelist)/(sender)/page/([01-9]\d*)',
'controllers.policyd.wblist.Whitelist',

# Whitelist: DNS name.
'/antispam/(whitelist)/(dnsname)',
'controllers.policyd.wblist.Whitelist',
'/antispam/(whitelist)/(dnsname)/%s' % re_dnsname,
'controllers.policyd.wblist.Whitelist',
'/antispam/(whitelist)/(dnsname)/page/([01-9]\d*)',
'controllers.policyd.wblist.Whitelist',

# Blacklist: IP addresses.
'/antispam/(blacklist)',
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(ip)',
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(ip)/%s' % re_ip,
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(ip)/page/([01-9]\d*)',
'controllers.policyd.wblist.Blacklist',

# Blacklist: Sender addresses.
'/antispam/(blacklist)/(sender)',
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(sender)/%s' % re_ip,
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(sender)/page/([01-9]\d*)',
'controllers.policyd.wblist.Blacklist',

# Blacklist: DNS name.
'/antispam/(blacklist)/(dnsname)',
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(dnsname)/%s' % re_dnsname,
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(dnsname)/page/([01-9]\d*)',
'controllers.policyd.wblist.Blacklist',

# Blacklist: DNS name.
'/antispam/(blacklist)/(helo)',
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(helo)/page/([01-9]\d*)',
'controllers.policyd.wblist.Blacklist',
'/antispam/(blacklist)/(helo)/(%s)' % re_dnsname,
'controllers.policyd.wblist.Blacklist',
)
----

Directory structure:

----
$ find controllers/policyd
controllers/policyd
controllers/policyd/__init__.py
controllers/policyd/urls.py
controllers/policyd/wblist.py
----

Content of controllers/policyd/wblist.py.
class Whitelist works fine, but class Blacklist doesn't work as
expected, error msg is pasted above.

----
import sys
import web
from web import ctx, render_policyd as render
from controllers import base
from libs.policyd import whitelist, blacklist

cfg = web.iredconfig
session = web.config.get('_session')

whitelistLib = whitelist.Whitelist()
blacklistLib = blacklist.Blacklist()

class Whitelist:
@base.require_global_admin
def GET(self, listname='whitelist', listcategory='ip',
cur_page=1,):
self.listname = web.safestr(listname)
self.listcategory = web.safestr(listcategory)
self.cur_page = int(cur_page)

total, entries = whitelistLib.list
(listcategory=self.listcategory, cur_page=self.cur_page,)

if self.cur_page > total and self.cur_page != 1:
# Redirect to last page if cur_page > total
if total % session.pagesizelimit > 0:
page = total/session.pagesizelimit + 1
else:
page = total/session.pagesizelimit
web.seeother(ctx.homepath + '/%s/%s/page/%d' % (listname,
listcategory, page))
else:
return render.wblist(
total=total,
entries=entries,
listname=self.listname,
listcategory=self.listcategory,
cur_page=self.cur_page,
)

class Blacklist:
@base.require_global_admin
def GET(self, listname='blacklist', listcategory='ip',
cur_page=1,):
self.listname = web.safestr(listname)
self.listcategory = web.safestr(listcategory)
self.cur_page = int(cur_page)

total, entries = blacklistLib.list
(listcategory=self.listcategory, cur_page=self.cur_page)

if self.cur_page > total and self.cur_page != 1:
# Redirect to last page if cur_page > total
if total % session.pagesizelimit > 0:
page = total/session.pagesizelimit + 1
else:
page = total/session.pagesizelimit
web.seeother(ctx.homepath + '/%s/%s/page/%d' % (listname,
listcategory, page))
else:
return render.wblist(
total=total,
entries=entries,
listname=self.listname,
listcategory=self.listcategory,
cur_page=self.cur_page,
)
----

Zhang Huangbin

unread,
Nov 15, 2009, 11:33:07 AM11/15/09
to web.py
Solved.

Move /page/xxx on the top of urls.py, fixed.

I think some regular expressions of url mapping override some others
which initialized later. :)

Zhang Huangbin

unread,
Nov 15, 2009, 11:39:56 AM11/15/09
to web.py
Oops, the issue was due to incorrect URL mapping:

----
'/antispam/(blacklist)/(dnsname)/%s' % re_dnsname,
----

I should as () in last '%s':

----
'/antispam/(blacklist)/(dnsname)/(%s)' % re_dnsname,
----

shi shaozhong

unread,
Nov 20, 2009, 12:30:00 PM11/20/09
to we...@googlegroups.com
What is the definition of url mapping?

Sincerely,

David

2009/11/15 Zhang Huangbin <michae...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages