Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django和jQuery/JSON-NmapService不可JSON序列化_Jquery_Python_Json_Django - Fatal编程技术网

Django和jQuery/JSON-NmapService不可JSON序列化

Django和jQuery/JSON-NmapService不可JSON序列化,jquery,python,json,django,Jquery,Python,Json,Django,我想为副本道歉,我在谷歌上搜索了一个半小时,但没有成功 情景 我有一个分析用户提交的文件的web应用程序。 我为模板中的每个主机添加了一个按钮,用于将单击的主机添加到会话中。在应用程序的后期阶段,会话中的每个主机都会使用ReportLab添加到reportgenerator 我当前拥有的功能可以正常工作,直到我单击包含NmapService对象的主机。 然后我得到了这个错误 回溯 Internal Server Error: /addToSession/ Traceback (most rece

我想为副本道歉,我在谷歌上搜索了一个半小时,但没有成功

情景
我有一个分析用户提交的文件的web应用程序。
我为模板中的每个主机添加了一个按钮,用于将单击的主机添加到会话中。在应用程序的后期阶段,会话中的每个主机都会使用ReportLab添加到reportgenerator

我当前拥有的功能可以正常工作,直到我单击包含NmapService对象的主机。
然后我得到了这个错误

回溯

Internal Server Error: /addToSession/
Traceback (most recent call last):
  File "<stripped>/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 235, in get_response
    response = middleware_method(request, response)
  File "<stripped>/local/lib/python2.7/site-packages/django/contrib/sessions/middleware.py", line 50, in process_response
    request.session.save()
  File "<stripped>/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py", line 82, in save
    obj = self.create_model_instance(data)
  File "<stripped>/local/lib/python2.7/site-packages/django/contrib/sessions/backends/db.py", line 68, in create_model_instance
    session_data=self.encode(data),
  File "<stripped>/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 88, in encode
    serialized = self.serializer().dumps(session_dict)
  File "<stripped>/local/lib/python2.7/site-packages/django/core/signing.py", line 95, in dumps
    return json.dumps(obj, separators=(',', ':')).encode('latin-1')
  File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: NmapService: [open 23/tcp telnet (product: Cisco IOS telnetd devicetype: switch ostype: IOS)] is not JSON serializable
[18/Dec/2015 14:18:51] "POST /addToSession/ HTTP/1.1" 500 17246
模板

{% extends 'base.html' %}
{% block content %}
<div class="container col-md-12">
    <div class="panel">
    <div class="panel panel-heading">
        <h1>Nmap scan results</h1>
    </div>
    {% for host in myList %}
    <div class="panel panel-body">
    <button class="btn btn-info add" host="{{host.id}}" onclick="addtoSession" >Add to session</button>
        <form class="form">
            {% csrf_token %}
            <h2>Scan results for {{host.id }}</h2>

            <hr>
            <h3>Algemene informatie</h3>
            <table class="table">
                <tr>
                    <td>
                        <p>
                            Host ipv4 : {{host.host_ipv4}}
                        </p>
                        <p>
                            host ipv6 : {{host.host_ipv6}}
                        </p>
                        <p>
                            host mac: {{host.host_mac}}
                        </p>
                    </td>
                    <td>
                        <p>
                            Host last boot: {{host.host_boot}}
                        </p>
                        <p>
                            Host uptime: {{host.host_uptime}}
                        </p>
                        <p>
                            Host os: {{host.host_os}}
                        </p>
                        <p>
                            Correctness: {{host.host_guess_percentage_os}}
                        </p>
                    </td>
                </tr>
            </table>
            <hr>
            <h3>Discovered services</h3>
            <table class="table">
                <thead>
                    <th>Protocol</th>
                    <th>Port</th>
                    <th>State</th>
                    <th>Service name</th>
                </thead>
                <tbody>
                    {% for x in host.host_services %}
                    <tr>
                        <td>{{x.protocol}}</td>
                        <td>{{x.port}}</td>
                        <td>{{x.state}}</td>
                        <td>{{x.service}}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            <hr>
            {% if host.host_scripts %}
            <h3>Scripts</h3>
            <table class="table">
                {% for script in host.host_scripts %}
                    <tr>
                        <td><span class="label label-warning">{{script.scriptName}}</span></td>
                        <td>{{script.scriptOutput}}</td>
                    </tr>
                {% endfor %}
            </table>
            <hr>
            {% endif %}
        </form>
    </div>
    {% endfor %}
</div>
</div>
{% endblock %}

不幸的是,jQuery的所有功能都没有用。
我还可以做些什么来实现上述场景。

有些对象是不可序列化的,就是这样。从对象中选取特定数据并序列化它们,而不是整个对象。是否有某种方法可以编写自己的序列化程序?重要的是我可以将数据传送到repor生成器。有些对象是不可序列化的,就是这样。从对象中选取特定数据并序列化它们,而不是整个对象。是否有某种方法可以编写自己的序列化程序?重要的是,我可以将数据传输到repor生成器。
{% extends 'base.html' %}
{% block content %}
<div class="container col-md-12">
    <div class="panel">
    <div class="panel panel-heading">
        <h1>Nmap scan results</h1>
    </div>
    {% for host in myList %}
    <div class="panel panel-body">
    <button class="btn btn-info add" host="{{host.id}}" onclick="addtoSession" >Add to session</button>
        <form class="form">
            {% csrf_token %}
            <h2>Scan results for {{host.id }}</h2>

            <hr>
            <h3>Algemene informatie</h3>
            <table class="table">
                <tr>
                    <td>
                        <p>
                            Host ipv4 : {{host.host_ipv4}}
                        </p>
                        <p>
                            host ipv6 : {{host.host_ipv6}}
                        </p>
                        <p>
                            host mac: {{host.host_mac}}
                        </p>
                    </td>
                    <td>
                        <p>
                            Host last boot: {{host.host_boot}}
                        </p>
                        <p>
                            Host uptime: {{host.host_uptime}}
                        </p>
                        <p>
                            Host os: {{host.host_os}}
                        </p>
                        <p>
                            Correctness: {{host.host_guess_percentage_os}}
                        </p>
                    </td>
                </tr>
            </table>
            <hr>
            <h3>Discovered services</h3>
            <table class="table">
                <thead>
                    <th>Protocol</th>
                    <th>Port</th>
                    <th>State</th>
                    <th>Service name</th>
                </thead>
                <tbody>
                    {% for x in host.host_services %}
                    <tr>
                        <td>{{x.protocol}}</td>
                        <td>{{x.port}}</td>
                        <td>{{x.state}}</td>
                        <td>{{x.service}}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            <hr>
            {% if host.host_scripts %}
            <h3>Scripts</h3>
            <table class="table">
                {% for script in host.host_scripts %}
                    <tr>
                        <td><span class="label label-warning">{{script.scriptName}}</span></td>
                        <td>{{script.scriptOutput}}</td>
                    </tr>
                {% endfor %}
            </table>
            <hr>
            {% endif %}
        </form>
    </div>
    {% endfor %}
</div>
</div>
{% endblock %}
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
//Get CSRF token
    function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for(var i = 0; i < cookies.length ; i++) {
            var cookie = jQuery.trim(cookies[i]);
            //Is this the cookie we want?
            if (cookie.substring(0, name.length +1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length +1));
                break;
            }
    }
}
    return cookieValue;
}
$('.add').click(function addtoSession(e) {
e.preventDefault();
$.ajax({
    type: "POST",
    url: "{% url 'addToSession' %}",
    data: {
        csrfmiddlewaretoken : getCookie('csrftoken'),
        "host" : $(this).attr("host"),
    },
    dataType:"json",
    success: function(data) {
        alert(json['message']);
    },
    error: function(rs,e) {
        alert(json['message']);
    }
});
});
</script>
host_services = []
if host.services:
for service in host.services:
        host_services.append(service)
        host_services = json.dumps(host_services, default=lambda o:o.__dict__, sort_keys=True, indent=4)
host_scripts = []
if host.scripts_results:
    for script in host.scripts_results:
        scripts = {}
        scripts['scriptName'] = script['id']
        scripts['scriptOutput'] = script['output']
        host_scripts.append(scripts)
    host_scripts = json.dumps(host_scripts, default=lambda o:o.__dict__, sort_keys=True, indent=4)