Skip to content

Instantly share code, notes, and snippets.

@toshimasa-nanaki
Created November 19, 2017 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshimasa-nanaki/aff704130eb7eca6728609b1d1f20a16 to your computer and use it in GitHub Desktop.
Save toshimasa-nanaki/aff704130eb7eca6728609b1d1f20a16 to your computer and use it in GitHub Desktop.
API を呼び出し(Python3 Ver)
from flask import Flask, render_template, request
import xmltodict
import urllib.request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', title='API TEST')
@app.route('/api', methods=['POST'])
def api():
baseUrl = "http://jvndb.jvn.jp/myjvn?"
name = baseUrl + "method=" + request.form['method'] + "&vulnId=" + request.form['vulnId']
req = urllib.request.Request(name)
with urllib.request.urlopen(req) as response:
xml = response.read()
dictionary = xmltodict.parse(xml)
import json
return json.dumps(dictionary)
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$('#submit').on("click", function() {
$.ajax({
url: "http://127.0.0.1:5000/api",
type:'POST',
dataType: 'json',
data : {"method": $("#method").val(), "vulnId": $("#vulnId").val()},
timeout:10000,
}).done(function(data) {
if(data["VULDEF-Document"] != undefined){
$("#detail").html(data["VULDEF-Document"].Vulinfo.VulinfoData.Title);
$("ul").empty();
}else{
$("ul").empty();
$("#detail").empty();
data["rdf:RDF"].item.forEach(function(element, index, array){
$("ul").append("<li>"+ element.title +"</li>");
})
}
$('#submit').prop("disabled", false);
}).fail(function(XMLHttpRequest, textStatus, errorThrown) {
alert("error");
$('#submit').prop("disabled", false);
})
});
});
</script>
<table border="0">
<tr>
<td align="right"><b> method:</b></td>
<td>
<select id="method">
<option value="getVulnOverviewList">getVulnOverviewList</option>
<option value="getVulnDetailInfo">getVulnDetailInfo</option>
</select>
</td>
</tr>
<tr>
<td align="right"><b> vulnId:</b></td>
<td><input type="text" name="vulnId" id="vulnId"></td>
</tr>
</table>
<input type="button" value="Submit" id="submit"/>
<div id="list">
<ul></ul>
</div>
<div id="detail"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment