简体   繁体   English

CGIHTTPRequestHandler 和 SimpleHTTPRequestHandler 在 Eclipse 在 Mac 在 Firefox

[英]CGIHTTPRequestHandler and SimpleHTTPRequestHandler in Eclipse on Mac in Firefox

Trying to run python cgi server from within Eclipse on a Mac Air and display hello world in Firefox, two problems.尝试在 Mac Air 上从 Eclipse 中运行 python cgi 服务器并在 Firefox 中显示 hello world,这有两个问题。 Here is the code to be run in a file run_server.py这是要在文件 run_server.py 中运行的代码

from http.server import HTTPServer
from http.server import CGIHTTPRequestHandler

def run_server(handler_class,server_class=HTTPServer ):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

def main():
    run_server(CGIHTTPRequestHandler)

if __name__ == '__main__':
    main()

Directory structure under project directory项目目录下的目录结构

analytics
         \
          run_server.py
cgi-bin
       \
        index.py

index.py索引.py

#!/usr/bin/env python 

print('Content-type: text/html;\n\n')
print('<h1>Hello, world!</h1>')

From a command line in the project directory, you can run with从项目目录中的命令行,您可以运行

$ python -m analytics.run_server

Be sure to have a __init__.py in the analytics directory to use -m option.确保分析目录中有一个 __init__.py 以使用 -m 选项。 Now try loading the page现在尝试加载页面

http://localhost:8000/cgi-bin/index.py

In Chrome, things work.在 Chrome 中,一切正常。 So what's the problem?所以有什么问题? In Firefox, the url is never found.在 Firefox 中,从未找到 url。 On a Mac also, if you run the server from inside the Eclipse IDE, the cgi tries to run python 3 code with OS installed python 2 Firefox 404s, times out, presents a blank page, or if the url problem is solved, tries to save the file. On a Mac also, if you run the server from inside the Eclipse IDE, the cgi tries to run python 3 code with OS installed python 2 Firefox 404s, times out, presents a blank page, or if the url problem is solved, tries to保存文件。 It doesn't serve static content as well, when the url problem exists.当 url 问题存在时,它也不提供 static 内容。 The Eclipse console in running the cgi, will display the stack trace of a syntax error from the python lib site.py print statement, tipping off the nature of the problem. Eclipse 控制台在运行 cgi 时,将显示来自 python lib site.py 打印语句的语法错误的堆栈跟踪,提示问题的性质。 See What's New in Python 3查看 Python 中的新增功能 3

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Simple solutions to both follow, in the answer section below.在下面的答案部分中,两者都遵循简单的解决方案。

Add to or create file /etc/localhosts, as in sudo vi /etc/localhosts, or editor of your choice.添加或创建文件 /etc/localhosts,如 sudo vi /etc/localhosts 或您选择的编辑器。

127.0.0.1 localhost

This fixes a known bug (link below) from two years ago, closed two months ago as a duplicate, and contains the above workaround, very near the bottom.这修复了两年前的一个已知错误(下面的链接),两个月前作为副本关闭,并包含上述解决方法,非常接近底部。 https://bugzilla.mozilla.org/show_bug.cgi?id=1433933 There is an active bug opened 5 years ago, updated 6 days ago, April 10, 2020, looks very near completion. https://bugzilla.mozilla.org/show_bug.cgi?id=1433933有一个 5 年前打开的活跃 bug,于 6 天前更新,2020 年 4 月 10 日,看起来非常接近完成。 https://bugzilla.mozilla.org/show_bug.cgi?id=1220810 Whenever it's done, you'll still have to update Firefox. https://bugzilla.mozilla.org/show_bug.cgi?id=1220810完成后,您仍然需要更新 Firefox。

For the conflict of python versions, running the server from the command line presents no problem in executing the cgi, index.py However, trying to run the server inside Eclipse doesn't work, when the shebang path #, (etymology sharp bang, or shell bang) is对于python版本冲突,从命令行运行服务器在执行cgi,index.py时没有问题但是,尝试在Eclipse里面运行服务器不起作用,当shebang路径#,(词源尖锐砰,或 shell 砰)是

#!/usr/bin/env python

On a Mac, it picks up the default python 2 installation, but is trying to run python 3 code (assuming you've upgraded since python 2 is past end of life).在 Mac 上,它会选择默认的 python 2 安装,但正在尝试运行 python 3 代码(假设您已升级,因为 python 2 已过期)。 Apple, despite OS updates has kept python 2 around for backwards compatability, though it's days are numbered.尽管操作系统更新,Apple 仍然保留 python 2 以实现向后兼容性,尽管它的日子已经屈指可数了。 Eventually the OS and applications will no longer rely on any OS installed python.最终操作系统和应用程序将不再依赖任何安装的操作系统 python。 Not sure of exact mechanism where Eclipse picks up python 2 when server runs script, though interpreter is 3. Setting path to #,/usr/bin/env python3 doesn't work.不确定当服务器运行脚本时 Eclipse 拾取 python 2 的确切机制,尽管解释器是 3。将路径设置为 #,/usr/bin/env python3 不起作用。 nor does #?/usr/bin/python3, What does, Find the absolute path to your python 3 and use that. #?/usr/bin/python3, What does, Find the absolute path to your python 3 并使用它。 it should appear in the error stack trace even, but here is an exampe from my setup.它甚至应该出现在错误堆栈跟踪中,但这是我的设置中的一个示例。

#./Library/Frameworks/Python.framework/Versions/3.6/bin/python3

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

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