[英]Python cgi script doesnot work properly
当我在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>
我真的不明白您为什么要像CGI那样做。 您无需进行任何服务器端处理,因此使用静态HTML文件更为合适。 (如果你打算做服务器端处理,你应该使用合适的模板系统。)
但是,如果将您的JS文件放在文件路径d:\\whatever
而不是Web URL实际上起作用,我会感到惊讶。
您需要运行一个cgi网络服务器来执行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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.