• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
CL.TANG
非官方言论,知识谨慎吸收
博客园    首页    新随笔    联系   管理    订阅  订阅

OpenStack 界面开发中response.body的中文编码问题

Contents [hide]
  • 1 问题的引入=
    • 1.1 解决办法
  • 2 用户限制输入中文
  • 3 不限制用户输入,呈现上修改

问题的引入=

G在我们创建虚拟机的时候,会设置虚拟机的名称,描述,如果没有限制用户输入,用户可以输入中文,会在页面呈现出乱码显示。

解决办法

用户限制输入中文

部分可行,但在用户自定义,如主机描述等必须可以输入中文的情况不适合。

不限制用户输入,呈现上修改

原来的实现方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
try:   
            response = httpclient.fetch(url,method=method, headers = headers)
        except Exception, e:
            return {"code":e.code}
        #{"hypervisors": [{"id": 1, "hypervisor_hostname": "node-7.domain.tld"}]}
 
        body = eval(response.body.replace("null", "None")).get("servers")
        servers = {}
        for b in body:
            print "*"*50
            ip_keys =  b["addresses"].keys()
            try:   
                for ip_key in ip_keys:
                    for address_ip in b["addresses"][ip_key]:
                        if address_ip["OS-EXT-IPS:type"] == "fixed":
                            print address_ip["addr"]
                            b.update({"ip_addr":address_ip["addr"]})
                            raise ValueError
            except:
                continue
        all_servers = {"servers":body}
        return all_servers

本身eval函数是有使用风险的。修改方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
try:   
            response = httpclient.fetch(url,method=method, headers = headers)
        except Exception, e:
            return {"code":e.code}
        #{"hypervisors": [{"id": 1, "hypervisor_hostname": "node-7.domain.tld"}]}
 
        #body = eval(response.body.replace("null", "None")).get("servers")
        body = json.loads(response.body)["servers"]
        servers = {}
        for b in body:
            print "*"*50
            ip_keys =  b["addresses"].keys()
            try:   
                for ip_key in ip_keys:
                    for address_ip in b["addresses"][ip_key]:
                        if address_ip["OS-EXT-IPS:type"] == "fixed":
                            print address_ip["addr"]
                            b.update({"ip_addr":address_ip["addr"]})
                            raise ValueError
            except:
                continue
        all_servers = {"servers":body}
        return all_servers

可以显示正常的中英文。然后在必须使用英文的地方加上输入限制。

posted @ 2016-10-18 14:00  CL.TANG  阅读(350)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3