简体   繁体   中英

How to run Python script on a webserver

I use a webapp that can generate a PDF report of some data stored in the app. To get to that report, however, requires several clicks and monkeying around with the app.

I support a group of users of this app (we use the app, we don't create the app) and I'd like them to be able to generate and view this report with as few clicks as possible. Thankfully, this web app provides a lot of data via a RESTful API. So I did some scripting.

I have a Python script that makes an HTTP GET request, processes the JSON results, and uses that resultant data to dynamically build a URL. Here's a simplified version of my python code:

#!/usr/bin/env python
import requests

app_id="12345"
secret="67890"
api_url='https://api.webapp.example/some_endpoint'

resp = requests.get(api_url, auth=(app_id,secret))
json_data = resp.json()

# Simplification of the data processing I'm doing
my_data = json_data['attr1']['attr2'] + my_data_processing

# Result of the script is a link to a dynamically generated PDF
pdf_url = 'https://pdf.webapp.example/items/' + my_data

The above is a simplification of the code I actually have, but it shows the relevant points. In my actual script, I continue on by doing another GET with the dynamically built URL. The webapp generates a PDF based on the my_data portion of the URL, and I write that PDF to file. This works very well today.

Currently, this is a python script that runs on my local machine on-demand. However, I'd like to host this somewhere on the web so that when a user hits a URL in their browser it runs and generates the pdf_url , instead of having to install this script on each user's local machine, and so that the PDF can be generated and viewed on a mobile device.

The thought is that the user can open http://example.com/report-shortcut , the python script would run server-side, dynamically build the URL, and redirect the user to that URL, which would then show the PDF in the browser (assuming the user is using a browser that shows PDFs like Chrome, Safari, etc). Alternately, if a redirect is problematic, going to http://example.com/report-shortcut could just show an HTML page with a link to the URL generated by the Python script.

I'm looking for a solution on how to host this Python script and have it run when a user accesses a webpage. I've looked into AWS Lambda and Django, but both seem like overkill for such a simple script (~20 lines of code, plus comments and whitespace). I've also looked at Python CGI scripting, which looks promising, but I have no experience setting up something like that.

Looking for suggestions on how best to host and run this code when a user goes to the example URL.

PS: I thought about just re-implementing in Javascript, but I'd rather the API key not be publicly accessible.

I suggest building the script in AWS Lambda and using the API Gateway to invoke it.

You could create the pdf, store it in S3 and generate a pre-signed URL. Then return a response 302 to the user to redirect them to the pre-signed URL. This will display the PDF in their browser. Very quick to setup and using Boto3 getting the PDF into S3 and generating the URL is simple.

It will be much simpler than some of your other suggestions.

See API Gateway & Boto3

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