I am working on an oauth project that requires me to have a callback url that can be loaded upon successful authorization in order to request access tokens, but I'm not sure how I can run that server in the proper manner. I am familiar with the useful one-line python server setup python -m http.server, but this will just load the directory in which I started the server and act as a server for navigating the files within that directory.
Is there a preferred way to set up a simple server that can be used for this redirect process and make the additional server call I need? Would I need to use a web framework like Django?
Using python -m http.server you can serve only static files but you need to run some code which gets argument(s) and uses it.
You could use option --cgi in python -m http.server --cgi and then you could put script (in any language) in folder cgi-bin and run it http://localhost/cgi-bin/script.py.
But method with CGI is very old and it can be much easier to use some of web microframework like Flask or bottle
script.py
from flask import Flask, request
app = Flask(__name__)
#app.route('/')
def index():
print('args:', request.args) # display text in console
#print('form:', request.form)
#print('data:', request.data)
#print('json:', request.json)
#print('files:', request.files)
return request.args.get('data', 'none') # send text to web browser
if __name__ == '__main__':
app.run(port=80, debug=True)
And run it as python script.py and test in web browser
http://127.0.0.1/?data=qwerty
And request.args.get("data") should gives you qwerty which you can use in Python code.
Related
I am using python version 3.8 and IIS 7.0. When I try to host my python web api on the IIS server it encounter with the FastCGI error. I have enable CGI in IIS and also added System.WebServer>>handlers>>Python FastCGI in my web config but still it gives same error. I have also checked the wfastcgi and flask are also successfully added.
You had to put both parameters in double quote separated by |
e.g.
"c:\python39\python.exe" | "c:\python39\Lib\site-packages\wfastcgi.py"
or put the whole path in "" after pasting it.
follow below steps to configure iis flask app in iis:
1)install python
2)after installing python install the wfastcgi. run the command prompt as administrator and run below command:
pip install wfastcgi
wfastcgi-enable
3)below is my flask example:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
app.run()
4)enable the cgi feature of iis:
5)open iis create a site.
6)after adding site select the site name and select the handler mapping feature from the middle pane.
Click “Add Module Mapping”
executable path value:
C:\Python37-32\python.exe|C:\Python37-32\Lib\site-packages\wfastcgi.py
Click “Request Restrictions”. Make sure “Invoke handler only if the request is mapped to:” checkbox is unchecked:
Click “Yes” here:
7)now go back and select the application setting feature.
click add from the action pane.
Set the PYTHONPATH variable(which is your site folder path):
And the WSGI_HANDLER (my Flask app is named app.py so the value is app.app — if yours is named site.py it would be site.app or similar):
8)Click OK and browse to your site.
Note: Do not forget to assign the iis_iusrs and iusr permission to the site folder and the python folder.
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 followed the tutorial here: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04 and created a Flask app hosted on an Ubuntu server on Digital Ocean. To update the website, I need to ssh into my cloud server and call "sudo reload myproject" from the command line. I would like to automate this and have the python code in Flask call this command every time it gets a post request.
I tried using os.system('sudo reload myproject') but that does not work. How would I use Flask to reload the project?
in /etc/sudoers.d/myOverrides.tmp, I have the following line:
user ALL=NOPASSWD: ALL
the code in my flask module:
from flask import Flask, render_template, request
import os
#application.route("/", methods=['GET', 'POST'])
def hello():
os.system('sudo reload halo')
if (request.method == 'POST'):
print 'post request'
os.system('sudo reload halo')
return "<h1>reloaded</h1>"
else:
return "<h1>default</h1>"
When I type in "sudo reload halo" on the command line, it reloads without a problem. I would like the Flask application to execute the same command.
This seems like a bad idea to me. Providing your web app sudo access and making that call publicly available screams liability.
Instead here are a couple of other options:
Automate it with a ssh command
You can actually run a command over SSH without having to do a full login yourself. You can continue to use the user you are using at the moment, but set up passwordless login to use a public-private key pair.
Then call ssh -i /path/to/keyfile username#server 'sudo reload halo'
Auto reload with gunicorn
gunicorn has a --reload option builtin you should consider using as well.
I asked something similar to this question and I haven't gotten any responses that help. So, I have decided to simplify things as much as I can with the following:
I have developed a python flask application and deployed to a beanstalk worker tier python environment. The issue is I can't figure out how to print or log or write anything anywhere. I need to debug this application and the only way I know how to do that is by printing to either the console or a log file to see exactly what is going on. When I run the application locally I can print to the console, write to files, and log with zero problems, it is just when I deploy it to the beanstalk environment that nothing happens. I have SSHed into the ec2 instance where I have application deployed and searched practically every file and I find that nothing was written by my python script anywhere.
This question probably seems absolutely stupid but can someone please provide me with an example of a python flask application that will run on a beanstalk worker environment that just prints "Hello World" to some file that I can find on the ec2 instance? Please include what should be written the requirements.txt file and any *.config files in the .ebextensions folder.
Thank You
Here is another simple python app that you can try. The one in the blog will work as well but this shows a minimal example of an app that prints messages received from SQS to a file on the EC2 instance.
Your app source folder should have the following files:
application.py
import os
import time
import flask
import json
application = flask.Flask(__name__)
start_time = time.time()
counter_file = '/tmp/worker_role.tmp'
#application.route('/', methods=['GET', 'POST'])
def hello_world():
if flask.request.method == 'POST':
with open(counter_file, 'a') as f:
f.write(flask.request.data + "\n")
return flask.Response(status=200)
if __name__ == '__main__':
application.run(host='0.0.0.0', debug=True)
requirements.txt
Flask==0.9
Werkzeug==0.8.3
.ebextensions/01-login.config
option_settings:
- namespace: aws:autoscaling:launchconfiguration
option_name: EC2KeyName
value: your-key-name
Launch a worker tier 1.1 environment with a Python 2.7 solution stack. I tested with (64bit Amazon Linux 2014.03 v1.0.4 running Python 2.7).
Wait for the environment to go green. After it goes green click on the queue URL as visible in the console. This will take you to the SQS console page. Right click on the queue and click on "Send a message". Then type the following message: {"hello" : "world"}.
SSH to the EC2 instance and open the file /tmp/worker_role.tmp. You should be able to see your message in this file.
Make sure you have IAM policies correctly configured for using Worker Role environments.
For more information on IAM policies refer this answer: https://stackoverflow.com/a/23942498/161628
There is a python+flask on beanstalk example on AWS Application Management Blog:
http://blogs.aws.amazon.com/application-management/post/Tx1Y8QSQRL1KQZC/Elastic-Beanstalk-Video-Tutorial-Worker-Tier
http://blogs.aws.amazon.com/application-management/post/Tx36JL4GPZR4G98/A-Sample-App-For-Startups
For the logging issues, i'd suggest:
Check your /var/log/eb-cfn-init.log (and other log files in this directory), if a .config command is failing you will see which and why there.
In your .config commands, output messages to a different log file so you see exactly where your bootstrap failed in your own file.
Add you application log file to EB Log Snapshots (/opt/elasticbeanstalk/tasks/taillogs.d/) and EB S3 log rotation (/opt/elasticbeanstalk/tasks/publishlogs.d/). See other files in these directories for examples.
I've got a website written in bottle and I'd like to deploy it via Amazon's Elastic Beanstalk. I followed the tutorial for deploying flask which I hoped would be similar.
I tried to adapt the instructions to bottle by making the requirements.txt this:
bottle==0.11.6
and replaced the basic flask version of the application.py file with this:
from bottle import route, run
#route('/')
def hello():
return "Hello World!"
run(host='0.0.0.0', debug=True)
I updated to this version as instructed in the tutorial, and when I wrote eb status it says it's green, but when I go to the URL nothing loads. It just hangs there. I tried the run() method at the end as it is shown above and also how it is written in the bottle hello world application (ie run(host='localhost', port=8080, debug=True)) and neither seemed to work. I also tried both #route('/hello') as well as the #route('/').
I went and did it with flask instead (ie exactly like the Amazon tutorial says) and it worked fine. Does that mean I can't use bottle with elastic beanstalk? Or is there something I can do to make it work?
Thanks a lot,
Alex
EDIT:
With aychedee's help, I eventually got it to work using the following application file:
from bottle import route, run, default_app
application = default_app()
#route('/')
def hello():
return "Hello bottle World!"
if __name__ == '__main__':
application.run(host='0.0.0.0', debug=True)
Is it possible that the WSGI server is looking for application variable inside application.py? That's how I understand it works for Flask.
application = bottle.default_app()
The application variable here is a WSGI application as specified in PEP 333. It's a callable that takes the environment and a start_response function. So the Flask, and Bottle WSGI application have exactly the same interface.
Possibly... But then I'm confused as to why you need that and the call to run.