Contents [hide] |
---|
问题的引入=
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
|
可以显示正常的中英文。然后在必须使用英文的地方加上输入限制。