摘要
今天在用curl调用一个solr地址查询数据的时候报错,发现是因为URL地址中含有
[ 中括号
导致的,既然shell的curl
命令不行,想着Python的urllib
包可以解析URL地址,那就尝试这个
curl 执行报错
错误的情况:
root@pts/3 $ curl "http://192.168.xxx.xxx:8983/solr/core1_shard2_replica1/select?q=*:*&fq=sex:1&fq=activeTime_tdt:[NOW-20MINUTE TO NOW]&wt=json&indent=true"
curl: (3) [globbing] error: bad range specification after pos 96
如果去掉中间包含中括号的查询条件则用curl没有问题
root@pts/3 $ curl "http://192.168.xxx.xxx:8983/solr/core1_shard2_replica1/select?q=*:*&fq=sex:1&wt=json&indent=true"
{
"responseHeader":{
"status":0,
"QTime":8,
"params":{
"q":"*:*",
"indent":"true",
"fq":"sex:1",
"wt":"json"}},
...
python urllib
这里给出具体的例子
root@pts/3 $ python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urlstr = "http://192.168.xxx.xxx:8983/solr/core1_shard2_replica1/select?q=*:*&fq=sex:1&fq=activeTime_tdt:[NOW-20MINUTE TO NOW]&wt=json&indent=true"
>>> result = urllib.urlopen(urlstr)
>>> print(result)
<addinfourl at 36354168 whose fp = <socket._fileobject object at 0x7f437fe03c50>>
>>> print(result.read())
{
"responseHeader":{
"status":0,
"QTime":11,
"params":{
"q":"*:*",
"indent":"true",
"fq":["sex:1",
"activeTime_tdt:[NOW-20MINUTE TO NOW]"],
"wt":"json"}},
最后可以结合Python的正则表达式来等来实现自己个性化的需求
公众号: DailyJobOps