简体   繁体   中英

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. Here is the code to be run in a file 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

#!/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. Now try loading the page

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

In Chrome, things work. So what's the problem? In Firefox, the url is never found. 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. It doesn't serve static content as well, when the url problem exists. 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. See What's New in 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.

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=1220810 Whenever it's done, you'll still have to update 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

#!/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). Apple, despite OS updates has kept python 2 around for backwards compatability, though it's days are numbered. Eventually the OS and applications will no longer rely on any OS installed 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. nor does #?/usr/bin/python3, What does, Find the absolute path to your python 3 and use that. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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