我是 django 和 python 的新手。我创建了一个 django Web 应用程序,我还有一个 python 脚本,我必须在 Web 应用程序的后端实时运行该脚本(就像它应该始终检查新更新并通过以下方式告诉 Web 有关 API 的新响应)生成通知)。我正在使用 IBM-Qradar API,我必须在 Web 应用程序上显示数据。
我有两个问题 1)有什么方法可以像上面描述的那样在我的 django 项目中使用下面的 python 代码吗? 2)并使用json格式的API响应将数据直接从响应变量存储到MySQL数据库中。
我只能找到使用表单将数据存储到数据库的方法,这对于我的项目来说不是必需的。
import json
import os
import sys
import importlib
sys.path.append(os.path.realpath('../modules'))
client_module = importlib.import_module('RestApiClient')
SampleUtilities = importlib.import_module('SampleUtilities')
def main():
# First we have to create our client
client = client_module.RestApiClient(version='9.0')
# -------------------------------------------------------------------------
# Basic 'GET'
# In this example we'll be using the GET endpoint of siem/offenses without
# any parameters. This will print absolutely everything it can find, every
# parameter of every offense.
# Send in the request
SampleUtilities.pretty_print_request(client, 'siem/offenses', 'GET')
response = client.call_api('siem/offenses', 'GET')
# Check if the success code was returned to ensure the call to the API was
# successful.
if (response.code != 200):
print('Failed to retrieve the list of offenses')
SampleUtilities.pretty_print_response(response)
sys.exit(1)
# Since the previous call had no parameters and response has a lot of text,
# we'll just print out the number of offenses
response_body = json.loads(response.read().decode('utf-8'))
print('Number of offenses retrieved: ' + str(len(response_body)))
# -------------------------------------------------------------------------
# Using the fields parameter with 'GET'
# If you just print out the result of a call to the siem/offenses GET
# endpoint there will be a lot of fields displayed which you have no
# interest in. Here, the fields parameter will make sure the only the
# fields you want are displayed for each offense.
# Setting a variable for all the fields that are to be displayed
fields = '''id%2Cstatus%2Cdescription%2Coffense_type%2Coffense_source%2Cmagnitude%2Csource_network%2Cdestination_networks%2Cassigned_to'''
# Send in the request
SampleUtilities.pretty_print_request(client, 'siem/offenses?fields='+fields, 'GET')
response = client.call_api('siem/offenses?fields=' +fields, 'GET')
# Once again, check the response code
if (response.code != 200):
print('Failed to retrieve list of offenses')
SampleUtilities.pretty_print_response(response)
sys.exit(1)
# This time we will print out the data itself
#SampleUtilities.pretty_print_response(response)
response_body = json.loads(response.read().decode('utf-8'))
print(response_body)
print(type(response_body))
for i in response_body:
print(i)
print("")
for j in response_body:
print(j['id'])
print(j['status'])
print(j['description'])
最佳答案
您可以编写自己的管理命令,并使用以下命令调用它们
myprojectpath/manage.py mycommand args ...
文档是here
请注意,如果您希望使用 Django 模型,这是必需的。这将在幕后为您完成许多环境设置。如果您尝试将 django 模型导入并使用到普通的 Python 脚本中,它们将无法正常工作。
关于mysql - 有没有办法用 django 项目运行 python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56945414/