下面的python脚本执行命令。该命令返回一个输出。输出如下所示。
我需要从输出中解析以下内容。
1)HTTP响应代码 2)点数组的内容。数组可以为空,也可以为非空数组(输出的最后一行)
输出:
Matched line for host akaa-wcb54zu2flskjhx4-szsapxzmalbot2fr.luna-dev.akamaiapis.net
{'access_token': 'akaa-r6gnip3mmql3evrj-mgiex4eajadrjltu', 'max-body': 131072, 'secret': 'yBc1LSGKBd6+sSP5tTR2o+YxlNYvlYNjWGdCFDl6Xt0=', 'signed-header': [], 'host': 'akaa-wcb54zu2flskjhx4-szsapxzmalbot2fr.luna-dev.akamaiapis.net', 'client_token': 'akaa-eyaa6fvcfngdlrb6-uxvn3hkpzxx33oxc'}
[]
Canonicalized header:
Auth data: EG1-HMAC-SHA256 client_token=akaa-eyaa6fvcfngdlrb6-uxvn3hkpzxx33oxc;access_token=akaa-r6gnip3mmql3evrj-mgiex4eajadrjltu;timestamp=20180410T16:38:24+0000;nonce=265e61d8-813c-4c3a-9edb-91caaa027420;
String-to-sign: POST https akaa-wcb54zu2flskjhx4-szsapxzmalbot2fr.luna-dev.akamaiapis.net /prolexic-analytics/v2/metrics YSycIdG2e9nY8KVLrPvMfzrBNMZEI8ncRxtcUP4h1J0= EG1-HMAC-SHA256 client_token=akaa-eyaa6fvcfngdlrb6-uxvn3hkpzxx33oxc;access_token=akaa-r6gnip3mmql3evrj-mgiex4eajadrjltu;timestamp=20180410T16:38:24+0000;nonce=265e61d8-813c-4c3a-9edb-91caaa027420;
args = ['curl', '-H', 'Authorization: EG1-HMAC-SHA256 client_token=akaa-eyaa6fvcfngdlrb6-uxvn3hkpzxx33oxc;access_token=akaa-r6gnip3mmql3evrj-mgiex4eajadrjltu;timestamp=20180410T16:38:24+0000;nonce=265e61d8-813c-4c3a-9edb-91caaa027420;signature=p6y+oDzaTfh3nj32R8jveDx8EGmjpHGybL7LJ0L+Adk=', '-S', '-i', '-k', '-X', 'POST', '-H', 'Host:akaa-wcb54zu2flskjhx4-szsapxzmalbot2fr.luna-dev.akamaiapis.net', '-H', 'Content-Type: application/json', '-H', 'Accept: application/json', '-d', '{"contract": "amade", "start": 1522420500, "end": 1522424147, "samples": 20, "type": {"connect": ["bandwidthIn"]}}', 'https://akaa-wcb54zu2flskjhx4-szsapxzmalbot2fr.luna-dev.akamaiapis.net/prolexic-analytics/v2/metrics', '-H', 'Expect:']
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 151
X-Trace-Id: c4db5acce87c20f3
Date: Tue, 10 Apr 2018 16:38:26 GMT
Connection: keep-alive
{"status":true,"currentContract":"amade","statusMsg":"Metrics acquired successfully","data":[{"service":"connect","metric":"bandwidthIn","points":[]}]}
脚本:
import subprocess
import sys
import re
n = sys.argv[1]
for i in range(int(n)):
# command to be executed
cmd = "python egcurl --eg-config .egcurl -Sik -X POST --eg-verbose -H 'Host:akaa-wcb54zu2flskjhx4-szsapxzmalbot2fr.luna-dev.akamaiapis.net' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{'contract': 'amade', 'start': 1522420500, 'end': 1522424147, 'samples': 20, 'type': {'connect': ['bandwidthIn']}}' https://akaa-wcb54zu2flskjhx4-szsapxzmalbot2fr.luna-dev.akamaiapis.net/prolexic-analytics/v2/metrics"
cmd_exec = subprocess.call(cmd, shell=True)
r1 = re.search("HTTP(.*)",cmd_exec)
print(r1)
print(cmd_exec)
错误:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 146, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
答案 0 :(得分:1)
问题是你在做什么
cmd_exec = subprocess.call(cmd, shell=True)
和 subprocess.call 返回结果代码,而不是输出。你想用
cmd_exec = subprocess.check_output(cmd, shell=True)
答案 1 :(得分:0)
你在这里问题是subproccess.call()
返回进程返回代码,而不是命令的输出。 From the docs:
运行args描述的命令。等待命令完成,然后 返回returncode属性
所以,您正试图在re.search()
上使用cmd_exec
(返回代码),这不是字符串,因此re
不知道如何处理它并抛出错误。
请参阅this answer,了解如何获取子过程命令的输出!