简体   繁体   English

将简单的Perl脚本翻译成可向客户端发送响应的Python?

[英]Translate simple Perl script into Python that sends a response to client?

I'm really new to Python and my goal is to have the Python script print something to the client, and then display this on my webpage. 我真的是Python新手,我的目标是让Python脚本向客户端打印一些内容,然后在网页上显示。

Fortunately, I stumbled upon a small code snippet that does exactly what I want to achieve with Python - unfortunately it is written in Perl. 幸运的是,我偶然发现了一个很小的代码片段,该片段完全可以实现我想用Python实现的功能-不幸的是,它是用Perl编写的。

I was wondering if anyone out there could show me how to write the Perl script in Python? 我想知道是否有人可以向我展示如何用Python编写Perl脚本?

Here is the link that has all the code: http://www.degraeve.com/reference/simple-ajax-example.php 这是包含所有代码的链接: http : //www.degraeve.com/reference/simple-ajax-example.php

Here is the Perl script: 这是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>";

How could I say the same thing in Python? 我如何在Python中说同样的话?

Here is the HTML page too: 这也是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>

Something like this should work. 这样的事情应该起作用。

#!/usr/bin/env python 

import cgi
import os
import cgitb; cgitb.enable()  # for troubleshooting

form = cgi.FieldStorage()
secretword = form.getfirst("w", "")
remotehost = cgi.escape(os.environ["REMOTE_HOST"] if "REMOTE_HOST" in os.environ else os.environ["REMOTE_ADDR"])

print "Content-Type: text/html"     
print # blank line, end of headers
print "<p>The secret word is <b>" + secretword + "</b> and your IP is <b>" + remotehost + "</b>.</p>"

Edit 1: How to list all environment variables. 编辑1:如何列出所有环境变量。

for k in os.environ.keys():
    print "<b>%20s</b>: %s<\br>" % (k, os.environ[k])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM