RESTful Python 路由 - 不工作?
RESTful Python Routes - not working?
RESTful routing 在 Python 中似乎不起作用。例如,
# Setup a mapper
from routes import Mapper
map = Mapper()
map.connect("add user", "/user", controller = "addUser", action = "add",
conditions=dict(method=["GET"]))
map.connect("post user", "/user", controller = "postUser", action = "post",
conditions=dict(method=["POST"]))
map.connect("update user", "/user/{id}", controller = "updateUser", action = "update",
conditions=dict(method=["GET"]))
map.connect("put user", "/user/{id}", controller = "putUser", action = "put",
conditions=dict(method=["PUT"]))
map.connect("delete user", "/user/{id}", controller = "deleteUser", action = "delete",
conditions=dict(method=["DELETE"]))
map.connect("home", "/", controller = "main", action = "index", conditions=dict(method=["GET"]))
# This is our application object. It could have any name,
# except when using mod_wsgi where it must be "application"
def application(environ, start_response):
# Get the request uri.
uri = environ.get('PATH_INFO', '')
req = uri if uri else '/'
# Match a URL, returns a dict or None if no match
mapped = map.match(req)
print mapped
# Everything done, return the response:
if mapped['action'] == 'index' :
response_body = index(environ, start_response, mapped)
elif mapped['action'] == 'add':
response_body = add(environ, start_response, mapped)
elif mapped['action'] == 'put':
response_body = put(environ, start_response, mapped)
elif mapped['action'] == 'delete':
response_body = delete(environ, start_response, mapped)
else:
response_body = "Not exist."
status = '200 OK'
response_headers = [('Content-Type', 'text/html'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
它不关心 REST 方法是 PUT、DELETE 还是 POST。它 仅在我的 url.
中查找 /user/{id}
的匹配项
因此,当我使用 http://127.0.0.1/index.py/user/1
发送 DELETE 方法时
我总是得到 {'action': u'update', 'controller': u'updateUser', 'id': u'1'}
这是 GET 从 map.connect("update user", "/user/{id}", controller = "updateUser", action = "update", conditions=dict(method=["GET"]))
知道我做错了什么吗?
编辑
Apache 配置文件,
LoadModule wsgi_module modules/mod_wsgi.so
....
<VirtualHost 127.0.0.1>
DocumentRoot "${path}/data/localweb"
ServerName 127.0.0.1
<Directory "${path}/data/localweb">
Options FollowSymLinks Indexes ExecCGI
AllowOverride All
Order deny,allow
Allow from 127.0.0.1
Deny from all
Require all granted
</Directory>
WSGIScriptAlias / C:\...\wsgi\route\basic\index.py
</VirtualHost>
顺便说一句,在我的 Apache 错误日志中,我总是在打印结果中收到此 wsgi:error
警告,
[Sat Aug 15 16:03:41.871541 2015] [wsgi:error] [pid 7068:tid 836]
{'action': u'update', 'controller': u'updateUser', 'id': u'1'}
我认为这可能是 http://pylonsbook.com/en/1.0/urls-routing-and-dispatch.html 的答案,这与我正在做的非常相似,
map.connect("add user", "/user", controller = "addUser", action = "add",
conditions=dict(method=["GET"]))
....
map.environ = {'REQUEST_METHOD':'DELETE'}
问题是我必须手动添加 DELETE 或 PUT ,这是 不 理想的,因为它不能是动态的。
编辑:
找到答案了!
request_method = environ.get('REQUEST_METHOD', '')
map.environ = {'REQUEST_METHOD': request_method}
RESTful routing 在 Python 中似乎不起作用。例如,
# Setup a mapper
from routes import Mapper
map = Mapper()
map.connect("add user", "/user", controller = "addUser", action = "add",
conditions=dict(method=["GET"]))
map.connect("post user", "/user", controller = "postUser", action = "post",
conditions=dict(method=["POST"]))
map.connect("update user", "/user/{id}", controller = "updateUser", action = "update",
conditions=dict(method=["GET"]))
map.connect("put user", "/user/{id}", controller = "putUser", action = "put",
conditions=dict(method=["PUT"]))
map.connect("delete user", "/user/{id}", controller = "deleteUser", action = "delete",
conditions=dict(method=["DELETE"]))
map.connect("home", "/", controller = "main", action = "index", conditions=dict(method=["GET"]))
# This is our application object. It could have any name,
# except when using mod_wsgi where it must be "application"
def application(environ, start_response):
# Get the request uri.
uri = environ.get('PATH_INFO', '')
req = uri if uri else '/'
# Match a URL, returns a dict or None if no match
mapped = map.match(req)
print mapped
# Everything done, return the response:
if mapped['action'] == 'index' :
response_body = index(environ, start_response, mapped)
elif mapped['action'] == 'add':
response_body = add(environ, start_response, mapped)
elif mapped['action'] == 'put':
response_body = put(environ, start_response, mapped)
elif mapped['action'] == 'delete':
response_body = delete(environ, start_response, mapped)
else:
response_body = "Not exist."
status = '200 OK'
response_headers = [('Content-Type', 'text/html'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
它不关心 REST 方法是 PUT、DELETE 还是 POST。它 仅在我的 url.
中查找/user/{id}
的匹配项
因此,当我使用 http://127.0.0.1/index.py/user/1
我总是得到 {'action': u'update', 'controller': u'updateUser', 'id': u'1'}
这是 GET 从 map.connect("update user", "/user/{id}", controller = "updateUser", action = "update", conditions=dict(method=["GET"]))
知道我做错了什么吗?
编辑
Apache 配置文件,
LoadModule wsgi_module modules/mod_wsgi.so
....
<VirtualHost 127.0.0.1>
DocumentRoot "${path}/data/localweb"
ServerName 127.0.0.1
<Directory "${path}/data/localweb">
Options FollowSymLinks Indexes ExecCGI
AllowOverride All
Order deny,allow
Allow from 127.0.0.1
Deny from all
Require all granted
</Directory>
WSGIScriptAlias / C:\...\wsgi\route\basic\index.py
</VirtualHost>
顺便说一句,在我的 Apache 错误日志中,我总是在打印结果中收到此 wsgi:error
警告,
[Sat Aug 15 16:03:41.871541 2015] [wsgi:error] [pid 7068:tid 836] {'action': u'update', 'controller': u'updateUser', 'id': u'1'}
我认为这可能是 http://pylonsbook.com/en/1.0/urls-routing-and-dispatch.html 的答案,这与我正在做的非常相似,
map.connect("add user", "/user", controller = "addUser", action = "add",
conditions=dict(method=["GET"]))
....
map.environ = {'REQUEST_METHOD':'DELETE'}
问题是我必须手动添加 DELETE 或 PUT ,这是 不 理想的,因为它不能是动态的。
编辑:
找到答案了!
request_method = environ.get('REQUEST_METHOD', '')
map.environ = {'REQUEST_METHOD': request_method}