我已经通过json为服务报告创建了一个html文件,它可以根据需要运行。
#Creates Html page
f = open("Oceaneering_Server_Status.html", "w")
f.write('''<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<style type=text/css>
body{{
background-color: #D9D8D5;
}}
table, th, td {{
border: 2px solid black;
border-collapse: collapse;
}}
th {{
text-align: center;
}}
td {{
text-align: center;
contains("STARTED").css('color', 'red');
}}
</style>
<body>
<h1>Oceaneering Server Status: </h1>
<input type="text" id="search" placeholder="Type to search">
<p>Last updated: {time}<br>Services Running: XXXX</br><p id="row"></br></p>
<table id="table" class="tablesorter" style="margin: 0px auto;" table class="sortable">>
<caption>Last updated: XXXX Total Services: XXXX Services Stopped: XXX Services Running: XXX </caption>
<col width="100">
<tr>
<th>Service</th>
<th>Folder</th>
<th>Service URL</th>
<th>Configured State</th>
<th>Real Time State</th>
<th>Server Type</th>
</tr>'''.format(time=date))
for item in json_read["folders"]:
services_url = "https://www.ocsdev.oceaneering.com/arcgis/admin/services/" + item + "/report?f=pjson&token=" + token
t = []
t.append(services_url)
print t
for i in t:
services_open = urllib.urlopen(services_url)
services_js = json.loads(services_open.read())
#print services_js
#z = open("test.text", "w")
for i in services_js["reports"]:
f = open("Oceaneering_Server_Status.html", "a")
line1 = "<tr>" + "\n\t<td>" + i["instances"]["serviceName"] + "</td>\n"
serv_count = i["instances"]["serviceName"]+ "\n"
line2 = "\t<td>" + i["instances"]["folderName"] + "</td>\n"
line3 = '\t<td><a href= "https://www.ocsdev.oceaneering.com/arcgis/rest/services/' + i["instances"]["folderName"] +"/"+ i["instances"]["serviceName"] + "/MapServer?f=jsapi&token=" + token + '">' + i["instances"]["serviceName"] + "</a>" + "</td>\n"
line4 = "\t<td>" + i["status"]["configuredState"] + "</td>\n"
line5 = "\t<td>" + i["status"]["realTimeState"] + "</td>\n"
line6 = "\t<td>" + i["instances"]["type"] + "</td>\n"
f.write(line1 + line2 + line3 + line4 + line5 + line6 )
#z.write(serv_count)
#z.close()
##with open('test.text') as b:
## aws = len(b.readlines())
##print aws
f.write('''</table>
<script>
$( "td:contains('STARTED')" ).css( "color", "green" );
$( "td:contains('STOPPED')" ).css( "color", "red" );
var $rows = $('#table tr');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
var tableSize = "Total Services: " + $('#table tr').length;
document.getElementById("row").innerHTML = tableSize;
</script>
</body>
</html>''')
f.close()
print "completed"
#_______________________________________________________________________________#
#Create TXT file of Services
import json
with open('Oceaneering_Server_Status.txt', 'w') as outfile:
json.dump(json_read, outfile)
print "completed"
我正在尝试创建'STOPPED'服务的txt文件。 我能够创建txt文件,但我似乎无法只打印我需要的特定服务... 想法?
答案 0 :(得分:0)
您应该只维护已停止服务的列表,在创建html文件时填充它并仅将其内容转储到JSON文件中:
#Creates Html page
f = open("Oceaneering_Server_Status.html", "w")
f.write('''<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<style type=text/css>
body{{
background-color: #D9D8D5;
}}
table, th, td {{
border: 2px solid black;
border-collapse: collapse;
}}
th {{
text-align: center;
}}
td {{
text-align: center;
contains("STARTED").css('color', 'red');
}}
</style>
<body>
<h1>Oceaneering Server Status: </h1>
<input type="text" id="search" placeholder="Type to search">
<p>Last updated: {time}<br>Services Running: XXXX</br><p id="row"></br></p>
<table id="table" class="tablesorter" style="margin: 0px auto;" table class="sortable">>
<caption>Last updated: XXXX Total Services: XXXX Services Stopped: XXX Services Running: XXX </caption>
<col width="100">
<tr>
<th>Service</th>
<th>Folder</th>
<th>Service URL</th>
<th>Configured State</th>
<th>Real Time State</th>
<th>Server Type</th>
</tr>'''.format(time=date))
stopped = []
for item in json_read["folders"]:
services_url = "https://www.ocsdev.oceaneering.com/arcgis/admin/services/" + item + "/report?f=pjson&token=" + token
t = []
t.append(services_url)
print t
for i in t:
services_open = urllib.urlopen(services_url)
services_js = json.loads(services_open.read())
#print services_js
#z = open("test.text", "w")
for i in services_js["reports"]:
f = open("Oceaneering_Server_Status.html", "a")
line1 = "<tr>" + "\n\t<td>" + i["instances"]["serviceName"] + "</td>\n"
serv_count = i["instances"]["serviceName"]+ "\n"
line2 = "\t<td>" + i["instances"]["folderName"] + "</td>\n"
line3 = '\t<td><a href= "https://www.ocsdev.oceaneering.com/arcgis/rest/services/' + i["instances"]["folderName"] +"/"+ i["instances"]["serviceName"] + "/MapServer?f=jsapi&token=" + token + '">' + i["instances"]["serviceName"] + "</a>" + "</td>\n"
line4 = "\t<td>" + i["status"]["configuredState"] + "</td>\n"
line5 = "\t<td>" + i["status"]["realTimeState"] + "</td>\n"
line6 = "\t<td>" + i["instances"]["type"] + "</td>\n"
f.write(line1 + line2 + line3 + line4 + line5 + line6 )
#z.write(serv_count)
if i["status"]["realTimeState"] == "STOPPED":
stopped.append(i)
#z.close()
##with open('test.text') as b:
## aws = len(b.readlines())
##print aws
f.write('''</table>
<script>
$( "td:contains('STARTED')" ).css( "color", "green" );
$( "td:contains('STOPPED')" ).css( "color", "red" );
var $rows = $('#table tr');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
var tableSize = "Total Services: " + $('#table tr').length;
document.getElementById("row").innerHTML = tableSize;
</script>
</body>
</html>''')
f.close()
print "completed"
#_______________________________________________________________________________#
#Create TXT file of Services
import json
with open('Oceaneering_Server_Status.txt', 'w') as outfile:
json.dump(stopped, outfile)
print "completed"