【发布时间】:2015-04-28 09:07:11
【问题描述】:
我对 Python 非常陌生,我的目标是让 Python 脚本向客户端打印一些内容,然后将其显示在我的网页上。
幸运的是,我偶然发现了一个小代码 sn-p,它完全可以用 Python 实现我想要实现的功能 - 不幸的是,它是用 Perl 编写的。
我想知道是否有人可以教我如何用 Python 编写 Perl 脚本?
这是包含所有代码的链接:http://www.degraeve.com/reference/simple-ajax-example.php
这是 Perl 脚本:
#!/usr/bin/perl -w
use CGI;
$query = new CGI;
$secretword = $query->param('w');
$remotehost = $query->remote_host();
print $query->header;
print "<p>The secret word is <b>$secretword</b> and your IP is <b>$remotehost</b>.</p>";
我怎么能在 Python 中说同样的话?
这里也是 HTML 页面:
<html>
<head>
<title>Simple Ajax Example</title>
<script language="Javascript">
function xmlhttpPost(strURL) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(getquerystring());
}
function getquerystring() {
var form = document.forms['f1'];
var word = form.word.value;
qstr = 'w=' + escape(word); // NOTE: no '?' before querystring
return qstr;
}
function updatepage(str){
document.getElementById("result").innerHTML = str;
}
</script>
</head>
<body>
<form name="f1">
<p>word: <input name="word" type="text">
<input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/cgi-bin/ajaxTest.pl")'></p>
<div id="result"></div>
</form>
</body>
</html>
【问题讨论】:
标签: javascript python ajax apache perl