Python Script In A Browser
Solution 1:
Actually you won't need too much. You'll need to deploy a web server that executes the script and the script should give the output as an HTTP response rather than writing to STDOUT
.
That being said, you can use Python's built in SimpleHTTPServer
for starters. It is a very basic web server (which can be improved) already written for you in Python's standard library. I'll rather use this for sysadmin and intranet tasks than Apache because it is very easy to set it up and start serving.
You'll probably need to extend it so when the request to run the script comes, it know what to do with it. SimpleHTTPServer
maybe is not a good fit here but you can extend BaseHTTPServer
or CGIHTTPServer
to accomplish the script execution.
On the script side you'll need to modify the output's target, nothing more clever than that. It'll probably require some refactoring but not much.
Keep in mind that BaseHTTPServer
is not focused on security, so use this in safe environments or the data of your company might be compromised.
I can get into any more details because the question is rather big but that is how I would start doing it.
Hope it helps!
Solution 2:
SimpleHTTPServer
is okay, but as its name implies, its quite simple. Try a microframework, like Flask
which includes a webserver and helpers to make things easier for you. The script couldn't be more simple:
from tools import thingamajig
from flask import Flask, requestapp= Flask(__name__)
@app.route('/go-go-gadget', methods=['POST','GET'])
def index():
if request.method == 'POST':
ip_address = request.form['ip_address']
results = thingamajig.do(ip_address)
return render('index.html', results=results)
return render('index.html')
if__name__== '__main__':
app.run()
Now just create a templates/
directory and in it, add a file index.html
with the following:
<form method="POST">
<inputtype="text" name="ip_address" />
<inputtype="submit">
</form>
{% if results %}
Here are the results: {{ results }}
{% endif %}
Now just python server.py
and go to http://localhost:5000/go-go-gadget
Post a Comment for "Python Script In A Browser"