我正在使用couchdb-python来访问我的 CouchDB 后端。有时我想知道库正在访问的 URL,以便使用curl
或任何其他工具访问完全相同的 URL。也就是说,我希望能够复制/粘贴编码的 URL 并执行 curl 访问,而无需手动编辑任何内容。
由于couchdb-python
没有给我这些信息,我构建了一个小工具来获取 URL:
import urllib
def get_couchdb_url(server, database, design_doc, view_name, **options):
prefix = 'http://%s:%d' % (server, 5984)
url = '/'.join([prefix, database, "_design", design_doc, '_view', view_name])
params = urllib.urlencode(options)
if params != "" : url += "?" + params
return url
number='+666666666'
print get_couchdb_url('localhost', 'phonenumbers', 'control', 'allgeonums', key = number)
这给了我以下网址:
http://localhost:5984/phonenumbers/_design/control/_view/allgeonums?key=%2B666666666
如果我使用 curl 访问,它会给出:
curl -X GET 'http://localhost:5984/phonenumbers/_design/control/_view/allgeonums?key=%2B666666666'
{"error":"bad_request","reason":"invalid_json"}
如果我手动编辑 URL,在键值之前和之后添加 %22,CouchDB 会接受它并给我正确的答案:
curl -X GET 'http://localhost:5984/phonenumbers/_design/control/_view/allgeonums?key=%22%2B666666666%22'
现在我有两个问题:
- Can
couchdb-python
show me the URL it is accessing? - How can I tell urllib.urlencode to produce encoded data which can be accepted by CouchDB? According to the manual, the values must be url encoded. I am not sure how to do this, for the general case. I would like to avoid having to read the
couchdb-python
sources for just this simple problem.