当我在我的python cgi脚本中生成html它没有显示!但是当我在IE中通过源代码打开这个空页面并复制到txt(html)文件然后通过IE打开它工作正常!无法理解为什么它如果生成的代码没问题就发生了 有人请,告诉我,我可以解决它或不是吗? 我的python脚本:
#!/usr/bin/env python
#-*-coding:utf-8-*-
import cgi
def select_graph_view():
print "Content-Type: text/html\n"
print '<html>'
print'<head>'
print('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">')
print('<title>Highcharts Example</title>')
print '<script type="text/javascript" src="d:\slider\web_server3\cgi-bin\js_css\jquery-1.11.0.min.js"></script>'
print'<script type="text/javascript">'
print'$(function () {'
print'$' + '('+"'#container').highcharts({"
print' title: {'
print" text: 'Monthly Average Temperature',"
print' x: -20 //center'
print' },'
print' subtitle: {'
print" text: 'Source: WorldClimate.com',"
print' x: -20'
print' },'
print' xAxis: {'
print" categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',"
print" 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']"
print' },'
print' yAxis: {'
print' title: {'
print" text: 'Temperature (°C)'"
print' },'
print' plotLines: [{'
print' value: 0,'
print' width: 1,'
print' color: '+"'#808080'"
print' }]'
print' },'
print' tooltip: {'
print" valueSuffix: '°C'"
print' },'
print' legend: {'
print" layout: 'vertical',"
print" align: 'right',"
print" verticalAlign: 'middle',"
print' borderWidth: 0'
print' },'
print' series: [{'
print" name: 'Tokyo',"
print' data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]'
print' }, {'
print" name: 'New York',"
print' data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]'
print' }, {'
print" name: 'Berlin',"
print' data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]'
print' }, {'
print" name: 'London',"
print' data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]'
print' }]'
print' });'
print'});'
print' </script>'
print'</head>'
print'<body>'
print'<script src="d:\slider\web_server3\cgi-bin\js_css\hightharts_js\highcharts.js"></script>'
print'<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>'
print'</body>'
print'</html>'
select_graph_view() <------------ call my def
和来自空生成页面的HTML代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="d:\slider\web_server3\cgi-bin\js_css\jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function () {
$('#container').highcharts({
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Temperature (?C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '?C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'Tokyo',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
name: 'New York',
data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
}, {
name: 'Berlin',
data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
}, {
name: 'London',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
}]
});
});
</script>
</head>
<body>
<script src="d:\slider\web_server3\cgi-bin\js_css\hightharts_js\highcharts.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</body>
</html>
答案 0 :(得分:0)
我真的不明白为什么你这样做就像CGI一样。您没有进行任何服务器端处理,因此静态HTML文件更合适。 (如果你 要进行服务器端处理,你应该使用一个合适的模板系统。)
但是,如果在文件路径上提供JS文件,我会感到惊讶 - d:\whatever
- 而不是网址实际上正在运行。
答案 1 :(得分:0)
你需要运行一个cgi webserver来执行python脚本。使用以下命令在根应用程序文件夹中创建cgi_testserver.py:
import CGIHTTPServer
import BaseHTTPServer
class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
cgi_directories = ["/cgi-bin"]
PORT = 5001
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
然后创建一个/ cgi-bin /目录,将你的python脚本放在那里并在HTML文件中调用它,如下所示:
http://localhost:5001/cgi-bin/name_of_python_script.py
而不是:
d:\slider\web_server3\cgi-bin\js_css\hightharts_js\highcharts.js