【问题标题】:CGIHTTPRequestHandler and SimpleHTTPRequestHandler in Eclipse on Mac in FirefoxFirefox 中 Mac 上的 Eclipse 中的 CGIHTTPRequestHandler 和 SimpleHTTPRequestHandler
【发布时间】:2020-04-16 22:07:46
【问题描述】:

尝试在 Mac Air 上的 Eclipse 中运行 python cgi 服务器并在 Firefox 中显示 hello world,两个问题。这是要在文件 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()

项目目录下的目录结构

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

index.py

#!/usr/bin/env python 

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

从项目目录中的命令行,你可以运行

$ python -m analytics.run_server

确保分析目录中有一个 __init__.py 以使用 -m 选项。现在尝试加载页面

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

在 Chrome 中,一切正常。所以有什么问题? 在 Firefox 中,永远找不到该 url。 同样在 Mac 上,如果您从 Eclipse IDE 内部运行服务器,cgi 会尝试运行安装了 OS 的 python 3 代码 python 2 Firefox 404s,超时,显示一个空白页面,或者如果 url 问题已解决,则尝试保存文件。当存在 url 问题时,它也不提供静态内容。 运行 cgi 的 Eclipse 控制台将显示来自 python lib site.py print 语句的语法错误的堆栈跟踪,提示问题的性质。查看 Python 3 的新特性

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

在下面的答案部分中遵循两者的简单解决方案。

【问题讨论】:

    标签: python eclipse firefox localhost cgi-bin


    【解决方案1】:

    添加或创建文件 /etc/localhosts ,如 sudo vi /etc/localhosts 或您选择的编辑器。

    127.0.0.1 localhost
    

    这修复了两年前的一个已知错误(下面的链接),两个月前作为副本关闭,并包含上述解决方法,非常接近底部。 https://bugzilla.mozilla.org/show_bug.cgi?id=1433933 有一个 5 年前打开的活跃 bug,6 天前更新,2020 年 4 月 10 日,看起来非常接近完成。 https://bugzilla.mozilla.org/show_bug.cgi?id=1220810 完成后,您仍然需要更新 Firefox。

    由于python版本的冲突,从命令行运行服务器在执行cgi,index.py时没有问题,但是尝试在Eclipse中运行服务器不起作用,当shebang路径#! (词源Sharp bang,或shell bang)是

    #!/usr/bin/env python

    在 Mac 上,它会选择默认的 python 2 安装,但会尝试运行 python 3 代码(假设您已经升级,因为 python 2 已经过时了)。尽管操作系统更新,Apple 仍然保留了 python 2 以实现向后兼容性,尽管它的日子已经屈指可数了。最终,操作系统和应用程序将不再依赖任何安装了 python 的操作系统。 不确定当服务器运行脚本时 Eclipse 选择 python 2 的确切机制,尽管解释器是 3。将路径设置为 #!/usr/bin/env python3 不起作用,#!/usr/bin/python3 也不起作用。有什么作用?找到你的 python 3 的绝对路径并使用它,它甚至应该出现在错误堆栈跟踪中,但这是我设置的一个示例。

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

    【讨论】:

      猜你喜欢
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 2015-01-31
      • 2012-11-23
      相关资源
      最近更新 更多