如何在python-apache-ubuntu服务器中将此代码作为网页运行?

时间:2022-10-06 09:44:52

This code snippet works in a normal python interpreter, just a simple binary search. What do I need to add/change to this code to run it as a webpage ?

此代码段在普通的python解释器中工作,只是一个简单的二进制搜索。我需要添加/更改此代码才能将其作为网页运行?

def binarySearch(s,array):
first=0;
last=len(array);
midpoint=first+(last-first)/2;
midpoint=int(midpoint);
#print(midpoint);
print (first,last,array[midpoint]);
if array[midpoint]==s: return (first,last,array[midpoint]);
while midpoint>1:
    if s>array[midpoint]:
        first=midpoint;
        print (first,last,array[midpoint]);
    elif s<array[midpoint]:
        last=midpoint;
        print (first,last,array[midpoint]);
    elif s==array[midpoint]:
        return (first,last,array[midpoint]);
    midpoint=first+(last-first)/2;
    midpoint=int(midpoint);
    #print(midpoint);

return None;

dArray=range(300);

print(binarySearch(90,dArray));

1 个解决方案

#1


"Altered to make it run in a webserver" doesn't really mean anything in this case. Your code (well, you have to fix it so it actually runs instead of raising exceptions, but let's pretend you did that) will already just work as a CGI or mod_python script. Everything that you print (or otherwise write to stdout) will become the response that a browser sees.

“改变以使其在网络服务器中运行”在这种情况下并不意味着什么。你的代码(好吧,你必须修复它以便它实际运行而不是引发异常,但让我们假装你这样做)将只是作为CGI或mod_python脚本工作。您打印(或以其他方式写入stdout)的所有内容都将成为浏览器看到的响应。

You may need to add a #!/usr/bin/env python3 or similar to the top of your program—just as you'd need to if you want to run it with ./hello.py in the shell instead of python3 hello.py. Then, it's just a matter of configuring your server. If you configure Apache to allow CGI in the /cgi-bin/ directory, and copy your script to that directory with mode 755, anyone who browses to http://myserver.example.com/cgi-bin/hello.py will get back the text Hello, world!. If you configure Apache to run .py scripts with mod_python instead of as CGIs, now you'll see the exact same thing, except maybe slightly faster or with slightly less load on your server.

您可能需要在程序顶部添加#!/ usr / bin / env python3或类似内容 - 如果您想在shell中使用./hello.py而不是python3 hello运行它,就像您需要的那样的.py。然后,只需配置您的服务器。如果将Apache配置为允许/ cgi-bin /目录中的CGI,并使用模式755将脚本复制到该目录,那么浏览到http://myserver.example.com/cgi-bin/hello.py的任何人都将获得回到文本你好,世界!如果你将Apache配置为使用mod_python而不是CGI来运行.py脚本,那么现在你会看到完全相同的东西,除非你的服务器稍微加快或负载稍微减少。

#1


"Altered to make it run in a webserver" doesn't really mean anything in this case. Your code (well, you have to fix it so it actually runs instead of raising exceptions, but let's pretend you did that) will already just work as a CGI or mod_python script. Everything that you print (or otherwise write to stdout) will become the response that a browser sees.

“改变以使其在网络服务器中运行”在这种情况下并不意味着什么。你的代码(好吧,你必须修复它以便它实际运行而不是引发异常,但让我们假装你这样做)将只是作为CGI或mod_python脚本工作。您打印(或以其他方式写入stdout)的所有内容都将成为浏览器看到的响应。

You may need to add a #!/usr/bin/env python3 or similar to the top of your program—just as you'd need to if you want to run it with ./hello.py in the shell instead of python3 hello.py. Then, it's just a matter of configuring your server. If you configure Apache to allow CGI in the /cgi-bin/ directory, and copy your script to that directory with mode 755, anyone who browses to http://myserver.example.com/cgi-bin/hello.py will get back the text Hello, world!. If you configure Apache to run .py scripts with mod_python instead of as CGIs, now you'll see the exact same thing, except maybe slightly faster or with slightly less load on your server.

您可能需要在程序顶部添加#!/ usr / bin / env python3或类似内容 - 如果您想在shell中使用./hello.py而不是python3 hello运行它,就像您需要的那样的.py。然后,只需配置您的服务器。如果将Apache配置为允许/ cgi-bin /目录中的CGI,并使用模式755将脚本复制到该目录,那么浏览到http://myserver.example.com/cgi-bin/hello.py的任何人都将获得回到文本你好,世界!如果你将Apache配置为使用mod_python而不是CGI来运行.py脚本,那么现在你会看到完全相同的东西,除非你的服务器稍微加快或负载稍微减少。