连接华为云资源nlp
本文是主要是记录博主连接nlp资源踩下的坑,以及分享一些注意事项.
使用的环境时ubuntu 16.04 +vscode +python 2.7
连接方法是用python构造函数,采用api相应的进行访问
准备篇
可以先看下官方的推荐文档,华为云nlp。推荐先使用插件操作之后,在进行相应的api的操作。官方推荐插件
获取项目的token
华为云提供两种方式访问nlp资源,token和ak/sk。对后一种连接方式不了解??要使用相应的签名算法,对token也是看了文档才明白是什么, 为了占个小便宜,我容易吗?
用python构造消息头与消息体并拿到token
先使用了requests库封装一个函数,在调用该函数用来实现访问api资源的功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
class HttpRequests():
'''对requests库封装的GET、POST、PUT等方法调用类'''
def __init__(self, url, data=None, type='GET', cookie=None, headers=None, proxies=None):
self.url = url
self.data = data
self.type = type
self.cookie = cookie
self.headers = headers
self.proxies = proxies
self.send_request()
def send_request(self):
'''setup a request'''
if self.url == None or self.url == '':
raise ('The url should not empty!')
if self.type == 'POST':
self.req = requests.post(self.url, data=self.data, headers=self.headers, proxies=self.proxies)
elif self.type == 'GET':
if self.data == None:
self.req = requests.get(self.url, headers=self.headers, proxies=self.proxies)
else:
self.req = requests.get(self.url, params=self.data, headers=self.headers, proxies=self.proxies)
elif self.type == 'PUT':
self.req = requests.put(self.url, data=self.data, headers=self.headers, proxies=self.proxies)
else:
self.req = None
raise ('The http request type NOT support now!')
def get_code(self):
try:
return self.req.status_code
except:
raise ('get code fail')
def get_url(self):
try:
return self.req.url
except:
raise ('get url fail')
def get_text(self):
try:
return self.req.text
except:
raise ('get text fail')
def get_headers(self):
try:
return self.req.headers
except:
raise ('get headers fail')
def get_cookie(self):
headers = self.get_headers()
if 'set-cookie' in headers:
return headers['set-cookie']
else:
return None
将拿取的保存在本地的token,用来构造接下来的消息头,代码如下。记得要更改相应的参数@两个用户名@密码@保存参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import json
import requests
from HttpRequest import HttpRequests
take_headers = {"content-type": "application/json"}
take_url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
data = {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "xxxxx",
"password": "xxxxxx",
"domain": {
"name": "xxxxx"
}
}
}
},
"scope": {
"project": {
"name": "cn-north-4"
}
}
}
}
req = HttpRequests(take_url, data=json.dumps(data), type="POST", headers =take_headers)
#print(req.get_code())
token_info = {
"token": req.get_headers()["X-Subject-Token"],
"project_id": "cn-north-4",
"expires_at": "expires_timestamp"
}
f = open('/home/danoao/文档/test.txt','w')
f.write(json.dumps(token_info))
f.close()
print(req.get_code())
print(req.get_headers())
如果以上代码运行的没有问题的话就可以看到,打印出来的状态码与消息头了。
将取出的token够造函数头取访问相应的api
写了一个类,用来调取nlu的那一部分资源。这里不得不吐嘈,!!!相应的url看的人脑袋晕乎乎的,{project_id}与{project_name}居然不是一个东西….代码如下 可以按需更该以下参数@url@call_data@地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import json
import requests
from HttpRequest import HttpRequests
import sys
reload(sys)
import time
sys.setdefaultencoding( "utf-8" )
headers = {"content-type": "application/json;charset=utf8"}
url = "https://nlp-ext.cn-north-4.myhuaweicloud.com/v1/0629bb1a768026c42fffc00e25759f39/nlu/"
class Clientnlu():
def __init__(self):
self.headers = {"content-type": "application/json;charset=utf8"}
self.get_token()
def get_token(self):
'''
获取保存在本地的token,配套另一python代码使用
具体可以访问网址
https://support.huaweicloud.com/api-iam/iam_30_0001.html
'''
try:
f = open("/home/danoao/文档/test.txt", 'r')
token_str = f.read().encode('utf-8')
f.close()
token_info = json.loads(token_str)
headers["x-auth-token"] = token_info["token"]
print('读取token成功')
except:
print('读取失败')
def classification(self,content):
'''
nlu的分类资源,具体可参见
https://support.huaweicloud.com/api-nlp/nlp_03_0017.html
'''
call_data={
"content":content,
"domain":1
}
class_url= url + "classification"
call_req = HttpRequests(class_url, data=json.dumps(call_data),
type="POST", headers =headers)
if(call_req.get_code() == 200):
a = call_req.get_text()
b= json.loads(a)
print("连接资源成功")
content = b["result"]["content"]
print(content)
label = b["result"]["label"]
#print(label)
confidence=b["result"]["confidence"]
#print(confidence)
else:
print(call_req.get_text())
if __name__ == "__main__":
x = Clientnlu()
while(1):
i = 1000
xx = str(i)
y =x.classification(xx)
time.sleep(1)
i = i-1
time.sleep(1)
#print(y)
总结
别说,只想占个便宜,哈哈哈最后的代码把额的意图露出来了,只想静静的水一水一个坐垫????小的代码迭代了6次。最终版本也刷了3次???
哈哈哈,期待华为云啥时候能开放一下更牛逼一些的nlu资源啊,期待中。