I'm trying to set up Python's CGIHTTPServer on Mac OS X to be able to serve CGI scripts locally, but I seem to be unable to do this.
I've got a simple test script:
#!/usr/bin/env python
import cgi
cgi.test()
It has permissions -rwxr-xr-x# and is located in ~/WWW (with permissions drwxr-xr-x). It runs just fine from the shell and I have this script to serve them using CGIHTTPServer:
import CGIHTTPServer
import BaseHTTPServer
class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
cgi_directories = ["~/WWW"]
PORT = 8000
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
But when I run it, going to localhost:8000 just serves the content of the script, not the result (i.e. it gives back the code, not the output).
What am I doing wrong?
The paths in cgi_directories are matched against the path part of the URL, not the actual filesystem path. Setting it to ["/"] or [""] will probably work better.
Related
Ive been trying to make a web server and I have the code down that should be able to get it running but when I go in to the Command Prompt and type python app.py it doesn't run when it should this is the code that I have
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main():
return "Welcome to my Flask page"
if __name__ == "__main__":
app.run(debug = True, host = "0.0.0.0", port=80)```
The server won't run on port 80, it will run on the default port (5000). If you run the server and navigate to HTTP://0.0.0.0:5000/, you should see your / response. See Why can't I change the host and port that my Flask app runs on?.
To change the port Flask runs on, you can specify it in the command line:
flask run -h localhost -p 3000
Here, I run the server on localhost:3000. If you try to run the server on port 80, you will get a permission denied error since any port under 1024 needs root privileges (as m1ghtfr3e said in their answer).
Also, this is a great tutorial I recommend to anyone learning flask https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
I think the problem is port 80.
Which OS are you using?
Ports under 1024 need root privileges, there is also a possibility that it is not working because some other service (like Apache) is running on this port.
So either fixing privileges or services or changing the port should make it run.
I have a collection of python scripts, that I would like to be able to execute with a button press, from a web browser.
Currently, I run python -m http.server 8000 to start a server on port 8000. It serves up html pages well, but that's about all it does. Is it possible to have it execute a python script (via ajax) and return the output, instead of just returning the full text of the .py file.
Additionally, if not, is there a simple (as in only 1 or 2 files) way to make this work? I'm looking for the equivalent of PHP -s, but for python.
For completeness, this is my html
<h1>Hello World</h1>
<button>
Click me!
</button>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.js"> </script>
<script>
$('button').click(function(){
$.get('/gui/run_bash.py');
});
</script>
Add --cgi to your command line.
python -m http.server --cgi 8000
Then place your python scripts in ./cgi-bin and mark them as executable.
$ mkdir cgi-bin
$ cp hello.py cgi-bin/hello.py
$ chmod +x cgi-bin/hello.py
You may need to slightly modify your python scripts to support the CGI protocol.
Here is the server running:
$ cat cgi-bin/hello.py
#! /usr/bin/env python3
print("Content-Type: application/json")
print()
print('{"hello": "world"}')
radams#wombat:/tmp/z/h$ python -m http.server --cgi
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [20/Mar/2018 18:04:16] "GET /cgi-bin/hello.py HTTP/1.1" 200 -
Reference: https://docs.python.org/3/library/http.server.html#http.server.CGIHTTPRequestHandler
http.server merely serves static files, it does not do any serverside processing or execute any code when you hit a python file. If you want to run some python code, you'll have to write an application to do that. Flask is a Python web framework that is probably well-suited to this task.
Your flask application might look something like this for executing scripts...
import subprocess
from flask import Flask
app = Flask(__name__)
SCRIPTS_ROOT = '/path/to/script_dir'
#app.route('/run/<script_name>')
def run_script(script_name):
fp = os.path.join(SCRIPTS_ROOT, script_name)
try:
output = subprocess.check_output(['python', fp])
except subprocess.CalledProcessError as call:
output = call.output # if exit code was non-zero
return output.encode('utf-8') # or your system encoding
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000)
And of course, I should include an obligatory warning 'having a webserver execute commands like this is insecure', etc, etc. Check out the Flask quickstart for more details.
I have simply created a python server with :
python -m SimpleHTTPServer
I had a .htaccess (I don't know if it is usefull with python server)
with:
AddHandler cgi-script .py
Options +ExecCGI
Now I am writing a simple python script :
#!/usr/bin/python
import cgitb
cgitb.enable()
print 'Content-type: text/html'
print '''
<html>
<head>
<title>My website</title>
</head>
<body>
<p>Here I am</p>
</body>
</html>
'''
I make test.py (name of my script) an executed file with:
chmod +x test.py
I am launching in firefox with this addres: (http : //) 0.0.0.0:8000/test.py
Problem, the script is not executed... I see the code in the web page...
And server error is:
localhost - - [25/Oct/2012 10:47:12] "GET / HTTP/1.1" 200 -
localhost - - [25/Oct/2012 10:47:13] code 404, message File not found
localhost - - [25/Oct/2012 10:47:13] "GET /favicon.ico HTTP/1.1" 404 -
How can I manage the execution of python code simply? Is it possible to write in a python server to execute the python script like with something like that:
import BaseHTTPServer
import CGIHTTPServer
httpd = BaseHTTPServer.HTTPServer(\
('localhost', 8123), \
CGIHTTPServer.CGIHTTPRequestHandler)
### here some code to say, hey please execute python script on the webserver... ;-)
httpd.serve_forever()
Or something else...
You are on the right track with CGIHTTPRequestHandler, as .htaccess files mean nothing to the the built-in http server. There is a CGIHTTPRequestHandler.cgi_directories variable that specifies the directories under which an executable file is considered a cgi script (here is the check itself). You should consider moving test.py to a cgi-bin or htbin directory and use the following script:
cgiserver.py:
#!/usr/bin/env python3
from http.server import CGIHTTPRequestHandler, HTTPServer
handler = CGIHTTPRequestHandler
handler.cgi_directories = ['/cgi-bin', '/htbin'] # this is the default
server = HTTPServer(('localhost', 8123), handler)
server.serve_forever()
cgi-bin/test.py:
#!/usr/bin/env python3
print('Content-type: text/html\n')
print('<title>Hello World</title>')
You should end up with:
|- cgiserver.py
|- cgi-bin/
` test.py
Run with python3 cgiserver.py and send requests to localhost:8123/cgi-bin/test.py. Cheers.
Have you tried using Flask? It's a lightweight server library that makes this really easy.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return '<title>Hello World</title>'
if __name__ == '__main__':
app.run(debug=True)
The return value, in this case <title>Hello World</title>, is rendered has HTML. You can also use HTML template files for more complex pages.
Here's a good, short, youtube tutorial that explains it better.
You can use a simpler approach and use the --cgi option launching the python3 version of http server:
python3 -m http.server --cgi
as pointed out by the command:
python3 -m http.server --help
I wrote a python XMLRPC server for my web application. The problem is whenever I start the server from shell and exit, xmlrpc server stops as well. I tried executing server script from another file thinking that it will continue to run in the background but that didn't work. Here's the code used to start a server.
host = 'localhost'
port = 8000
server = SimpleXMLRPCServer.SimpleXMLRPCServer((host, port))
server.register_function(getList)
server.serve_forever()
In the shell I just do >>python MyXmlrpcServer.py to start a server.
What do I do to be able to start a server and keep it running?
#warwaruk makes a useful suggestion; Twisted XML-RPC is simple and robust. However, if you simply want to run and manage a python process in the 'background' take a look at Supervisord. It is a simple process management system.
$ pip install supervisor
$ echo_supervisord_conf > /etc/supervisord.conf
Edit that config file to add a definition of your process thus...
[program:mycoolproc]
directory=/path/to/my/script/dir
command=python MyXmlrpcServer.py
Start supervisord and start your process
$ supervisord
$ supervisorctl start mycoolproc
Better use twisted to create an XML-RPC server. Thus you will not need writing your own server, it is very flexible, and you will be able to run in background using twistd:
#!/usr/bin/env python
import time, datetime, os, sys
from twisted.web import xmlrpc, server
from twisted.internet import reactor
class Worker(xmlrpc.XMLRPC):
def xmlrpc_test(self):
print 'test called!'
port = 1235
r = Worker(allowNone=True)
if __name__ == '__main__':
print 'Listening on port', port
reactor.listenTCP(port, server.Site(r))
reactor.run()
else: # run the worker as a twistd service application: twistd -y xmlrpc_server.py --no_save
from twisted.application import service, internet
application = service.Application('xmlrpc_server')
reactor.listenTCP(port, server.Site(r))
reactor.run()
#internet.TCPServer(port, server.Site(r)).setServiceParent(application)
I have the following code as my python server:
#!/usr/bin/python3
from http.server import HTTPServer, CGIHTTPRequestHandler
port = 8080
host_name = "localhost"
httpd = HTTPServer((host_name, port), CGIHTTPRequestHandler)
print("server started, to quit press <ctrl-c>")
httpd.serve_forever()
How do you set the DocumentRoot to which the server is serving the pages from.
The built-in CGIHTTPRequestHandler class serves from the current working directory, which is normally the directory from which you invoked Python.
This class is used to serve either files or output of CGI scripts from the current directory and below.
You can use os.chdir() to change the current working directory.
When you handle the GET request, you need to translate that into a path relative to the current directory the script is running in.
Look at http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer and the do_GET section. You should be able to adapt that for your purposes