有什么方法可以在django项目中运行python脚本吗?

时间:2019-07-09 04:36:14

标签: mysql json django python-3.x

我是django和python的新手。我创建了一个django Web应用程序,还有一个python脚本,我必须在Web应用程序的后端实时运行该脚本(就像它应该始终检查新更新并通过向Web告知来自API的新响应一样)生成通知)。我正在使用IBM-Qradar API,必须从该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'])

1 个答案:

答案 0 :(得分:0)

您可以编写自己的管理命令,并使用来调用它们

myprojectpath/manage.py mycommand args ...

文档为here

请注意,如果您希望使用Django模型,则这是必需的。有很多环境设置可以在后台为您完成。如果您只是尝试将django模型导入并使用到普通的Python脚本中,则它们将无法正常工作。