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
Related
I'm going through the "Linux Fundamentals" section on TryHackMe, and I was asked to start a web server using Python's "HTTPServer" module and download a file from said server. My first line of code was python3 -m http.server to initiate the server, then in the Linux terminal all I had was a blank line, no directory or prompt or anything. I input wget http://ip.address:port/filename with the correct IP address, port, and filename but nothing ran. It took me opening another terminal window in order to download the file, is this correct or have I missed something? This is my first introduction to Linux so I apologise if I've missed something simple.
I am working on an django application and have kind of finish coding it.
This app is designed to be used by someone else and as she isn't so familiar with command line or so, i would like to create an executable (with pyInstaller) that start the localhost server and open the page on a browser (I don't nee to deploy it online or so, the localhost is sufficient).
To do so, I've created a python script that calls the command
python3 manage.py runserver
and then open the localhost page in a browser.
I then called the command
pyinstaller myApp.py
(with myApp.py the python script containing the 2 commands above) and then also the command
pyinstaller TestMACB2.spec
(I'm not sure I need this command but anyway I now have an executable that tries to run those two commands).
The opening of the web page works well but I dont know why for the command to start the server, i have this error:
Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
I do have django installed and when I start the server with the command in a terminal, it works fine.
This is myApp.py script :
import os
import sys
import webbrowser
os.system('python3 <full_path_to_manage.py> runserver')
webbrowser.open('http://127.0.0.1:8000/MACB')
Do you have an idea how to fix it ?
I also have another little issue : for the command os.system('python3 <full_path_to_manage.py> runserver') i must specify the full path. If i try something like ./manage.py or ./../../../manage.py it says it cant find this file. but in the second case it should work. do you know how I could avoid using the full path ?
Thank you for the time you took reading this!
I am trying to run a local CGI server on my raspberry pi to host a webpage with a single link, that link is to a CGI script which is supposed to trigger another script and then print HTML code to redirect back to the starting page (so that it doesn't hang)
in the servers root directory i have:
index.html
favicon.ico
Server.py
cgi-bin
my server is set up to use the cgi-bin folder for cgi-scripts.
the issue i am having is i cannot seem to make the scripts callable, is so instead of typing "python Server.py" i should be able to type "Server.py"
in order to do this i have tried multiple shebangs:
#!/usr/bin/env python
#!/usr/bin/python
and then called chmod a+x Server.py to mark it as executable, to no avail.
to clarify i am using:
python 2.7.3rc2
standard raspi linux distro "wheezy"
i read in some of the help docs that if the file has DOS style newlines it interferes with the shebang, so i have ensured that they are now MAC style newlines, this still did not work.
to test further i have made a simple python file which contains:
#!/usr/bin/python
print "Hello World!"
saved it as test.py, marked it as executable, and tried:
/test.py
from the command line and i get:
print: bad interpreter: No such file or directory
can someone please tell me where i'm going wrong?
Thanks
James
Try to remove windows line endings in the script. This made it work for me.
E.g. see How to convert Windows end of line in Unix end of line (CR/LF to LF)
For more possible causes of this problem have look at my answer here https://stackoverflow.com/a/65249192/1150303
LSOpenURLsWithRole() failed with error -600 for the URL http://localhost:9000/.
This is the error I get when I try to launch my SimpleHTTPServer while in a tmux session. I'm a front-end web developer and I spend most of my time working with a SimpleHTTPServer, rather than Apache. The issue is that it errors out at the open command, because I have the habit of opening files and directories from the terminal directly (open dirname/, or open .) , and when i use this in tmux it gives me the same error.
I want to mention that I'm on a Macbook Air, running OSX 10.9 Mavericks.
This is the code of the function I use in my terminal to start the server:
# Start an HTTP server from a directory, optionally specifying the port
function server() {
local port="${1:-8000}"
open "http://localhost:${port}/"
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
Edit
The issue doesn't appear anymore so I have 3 possible solutions for this:
Highly unlikely:
changing from Python3 to Python 2.7.5 (OS X Default)
Most likely:
Apple released an update to Mavericks that fixes this issue
installing Command-Line tools in order to use Homebrew to build & install the latest version of VIM
Not sure if this is the same case for you, since you mentioned you restarted a few times ...
However for me, I noticed that I had stray 2 tmux sessions after an iterm failure, which I had forgotten about. They were running a bunch of services started by grunt, so i'm assuming that one of these services was conflicting when trying to start again. Killing them made the bug not occur anymore and I was able to run my node app in tmux.
Like #Cosmin said, check if there is another tmux running, kill all tmux.
Then open a new tmux, it will work like before.
It is pretty straight forward I run the python web2py.py in terminal (mac ox lion) and go to my controller. Before I had code which I thought was doing it, but now I only have a this left in my code and I'm still getting the problem:
def login():
import pdb
pdb.set_trace()
value = 'test'
return dict(test=value)
From this I'm then getting:
/Users/auser/web/public/applications/api/controllers/profile.py(10)login()
-> value = 'test'
(Pdb) Tcl_WaitForEvent: Notifier not initialized
Abort trap: 6
Does anyone know what is going on here. I'm using the standard flavour of python which comes with the Mac OSX - Python 2.7.1
The system hangs for a split second and then spits out the Abort Trap: 6. Then I get the "do you want to report this problem with python" suspect from mac osx because it thinks the world blew up. I don't know if web2py has a kill switch or what for pdb, however it is driving me insane.
pdb works fine when web2py is not involved I've tested that also.
I don't want to download winpdb as I have to switch the python environment (64 to 32) I'm in and I don't really want to do that and create another headache. Especially when it works without web2py?
Any help will be greatly appreciated.
Cheers,
Sententia
I was going about it the wrong way. It must be trying to freeze the web2py server and causing a critical fault.
The way I'm using it now is in interactive shell mode which fixes the server environment problem - I can't believe I didn't try this earlier.
I use the same code:
def login():
import pdb
pdb.set_trace()
value = 'test'
return dict(test=value)
and use this at the terminal
python web2py.py --shell=/api/profile/login -M
It then breaks as it should.
Rage calming.....