我想将 运行 Python 的结果保存到文件中
I want to save the results of running Python to a file
import requests, json, pprint
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
pagesize = 1000
api_url_base = "https://test/sepm/api/v1/"
authentication_url = "https://test/sepm/api/v1/identity/authenticate"
json_format = True
payload = {
"username" : "test",
"password" : "test",
"domain" : ""}
headers = {"Content-Type":"application/json"}
r = requests.post(authentication_url, verify=False, headers=headers,
data=json.dumps(payload))
api_token = (r.json()["token"])
headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer
{0}'.format(api_token)}
def get_info(url,params):
api_url = url
params = params
response = requests.get(api_url, headers=headers,verify=False,
params=params)
if response.status_code == 200:
return json.loads(response.content.decode('utf-8'))
else:
return response.status_code
def aggregate(endpoint_info,numberOfElements):
itr =0
while itr <= (numberOfElements-1):
computerName=endpoints_info['content'][itr]['computerName']
ipAddresses=endpoints_info['content'][itr]['ipAddresses'][0]
logonUserName=endpoints_info['content'][itr]['logonUserName']
lastUpdateTime=endpoints_info['content'][itr]['creationTime']
agentVersion = endpoints_info['content'][itr]['agentVersion']
print(computerName, ipAddresses, logonUserName, lastUpdateTime, a
gentVersion)
itr = itr+1
groups_url = '{0}groups'.format(api_url_base)
fingerprint_url = '{0}policy-objects/fingerprints'.format(api_url_base)
endpoints_url = '{0}computers?'.format(api_url_base)
total_pages = get_info(endpoints_url,{'pageSize':pagesize})['totalPages']
itr = 1
while itr <= total_pages:
params = {'pageSize':pagesize, 'pageIndex':itr}
endpoints_info = get_info(endpoints_url,params)
numberOfElements = endpoints_info['numberOfElements']
itr = itr +1
if endpoints_info is not 200:
aggregate(endpoints_info,numberOfElements)
else:
print('[!] Request Failed, {0}')
这是使用 Symantec rest API 的代码。
当你 运行 这个
可以得到列表格式的结果如下图
命令行输出
P09PC 123.63.40.37 测试 9 1520236609428 14.0.3897.1101
P10PC 123.63.40.31 测试 10 1520230270130 14.0.3775.1002
P11PC 123.63.40.27 测试 11 1520229680645 14.0.3775.1002
P12PC 123.63.40.26 测试 12 1520229515250 14.0.3775.1002
我修改了这个源,我想将结果保存到一个文件中。
不幸的是,几天的努力都失败了。
告诉我如何将它保存为文件
您的意思是不是要在终端上打印而是要将其写入文件?
过程很简单,只需以所需模式(追加模式或写入模式,具体取决于您的用例)打开一个文件,然后将其写入那里即可。
考虑阅读 this tutorial 以掌握概念。
初始化文件对象后(如教程中所述)您只需要在文件中写入而不是打印语句。代码看起来像这样
file = open('output.txt', 'w')
file.write(computerName +' '+ ipAddresses +' '+ logonUserName +' '+ lastUpdateTime +' '+ agentVersion)
file.close()
import requests, json, pprint
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
pagesize = 1000
api_url_base = "https://test/sepm/api/v1/"
authentication_url = "https://test/sepm/api/v1/identity/authenticate"
json_format = True
payload = {
"username" : "test",
"password" : "test",
"domain" : ""}
headers = {"Content-Type":"application/json"}
r = requests.post(authentication_url, verify=False, headers=headers,
data=json.dumps(payload))
api_token = (r.json()["token"])
headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer
{0}'.format(api_token)}
def get_info(url,params):
api_url = url
params = params
response = requests.get(api_url, headers=headers,verify=False,
params=params)
if response.status_code == 200:
return json.loads(response.content.decode('utf-8'))
else:
return response.status_code
def aggregate(endpoint_info,numberOfElements):
itr =0
while itr <= (numberOfElements-1):
computerName=endpoints_info['content'][itr]['computerName']
ipAddresses=endpoints_info['content'][itr]['ipAddresses'][0]
logonUserName=endpoints_info['content'][itr]['logonUserName']
lastUpdateTime=endpoints_info['content'][itr]['creationTime']
agentVersion = endpoints_info['content'][itr]['agentVersion']
print(computerName, ipAddresses, logonUserName, lastUpdateTime, a
gentVersion)
itr = itr+1
groups_url = '{0}groups'.format(api_url_base)
fingerprint_url = '{0}policy-objects/fingerprints'.format(api_url_base)
endpoints_url = '{0}computers?'.format(api_url_base)
total_pages = get_info(endpoints_url,{'pageSize':pagesize})['totalPages']
itr = 1
while itr <= total_pages:
params = {'pageSize':pagesize, 'pageIndex':itr}
endpoints_info = get_info(endpoints_url,params)
numberOfElements = endpoints_info['numberOfElements']
itr = itr +1
if endpoints_info is not 200:
aggregate(endpoints_info,numberOfElements)
else:
print('[!] Request Failed, {0}')
这是使用 Symantec rest API 的代码。 当你 运行 这个 可以得到列表格式的结果如下图
命令行输出 P09PC 123.63.40.37 测试 9 1520236609428 14.0.3897.1101 P10PC 123.63.40.31 测试 10 1520230270130 14.0.3775.1002 P11PC 123.63.40.27 测试 11 1520229680645 14.0.3775.1002 P12PC 123.63.40.26 测试 12 1520229515250 14.0.3775.1002
我修改了这个源,我想将结果保存到一个文件中。 不幸的是,几天的努力都失败了。 告诉我如何将它保存为文件
您的意思是不是要在终端上打印而是要将其写入文件?
过程很简单,只需以所需模式(追加模式或写入模式,具体取决于您的用例)打开一个文件,然后将其写入那里即可。
考虑阅读 this tutorial 以掌握概念。
初始化文件对象后(如教程中所述)您只需要在文件中写入而不是打印语句。代码看起来像这样
file = open('output.txt', 'w')
file.write(computerName +' '+ ipAddresses +' '+ logonUserName +' '+ lastUpdateTime +' '+ agentVersion)
file.close()