小编Sha*_*ner的帖子

使用CherryPY MethodDispatcher的动态URL

我需要配置支持以下URL方案的RESTful样式URL:

  • /父母/
  • /父/ 1
  • /父母/ 1 /儿童
  • /父母/ 1/chidren/1

我想使用MethodDispatcher,以便上面的每一个都可以有GET/POST/PUT/DELETE函数.我让它为第一个和第二个工作,但无法弄清楚如何配置子部分的调度程序.我有这本书,但它几乎没有涵盖这个,我在网上找不到任何样本.

这是我当前配置MethodDispatcher的方法.

root = Root()
conf = {'/' : {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}}    

cherrypy.quickstart(root, '/parent', config=conf)
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

python rest cherrypy

10
推荐指数
1
解决办法
5649
查看次数

Pythonic URL解析

关于如何在Python中解析URL有很多问题,这个问题是关于最佳或最恐怖的方法.

在我的解析中,我需要4个部分:网络位置,URL的第一部分,路径和文件名以及查询字符串部分.

http://www.somesite.com/base/first/second/third/fourth/foo.html?abc=123

应该解析成:

netloc = 'www.somesite.com'
baseURL = 'base'
path = '/first/second/third/fourth/'
file = 'foo.html?abc=123'
Run Code Online (Sandbox Code Playgroud)

下面的代码产生了正确的结果,但有没有更好的方法在Python中执行此操作?

url = "http://www.somesite.com/base/first/second/third/fourth/foo.html?abc=123"

file=  url.rpartition('/')[2]
netloc = urlparse(url)[1]
pathParts = path.split('/')
baseURL = pathParts[1]

partCount = len(pathParts) - 1

path = "/"
for i in range(2, partCount):
    path += pathParts[i] + "/"


print 'baseURL= ' + baseURL
print 'path= ' + path
print 'file= ' + file
print 'netloc= ' + netloc
Run Code Online (Sandbox Code Playgroud)

python url

0
推荐指数
1
解决办法
1116
查看次数

标签 统计

python ×2

cherrypy ×1

rest ×1

url ×1