1

I've got a bit of webdev experience, and just started learning python. I've created a python script that takes in an argument, and then runs a program, and prints print statements into the console. I'm curious if it's possible to put this program behind a webpage.

i.e. Webpage with a dropdown, chose the option and click a "Go" button. Then the python script is run with the option you chose, and your report is shown on the page.

I've looked at a few similar posts: Execute a python script on button click

run python script with html button

but they seem to offer very different solutions, and people seem to think the PhP idea is insecure. I'm also looking for something a bit more explicit. I'm fine to change the python script to return ajson or something instead of just print statements.

Community
  • 1
  • 1
singmotor
  • 3,930
  • 12
  • 45
  • 79

1 Answers1

1

A pretty common way to communicate between a webpage and a python program is to run the python as a WSGI server. Effectively the python program is a separate server which communicates with the webpage using GETs and POSTs.

One nice thing about this approach is that it decouples the Python app from the the web-page proper. You can test it by sending http requests directly to a test server while you're developing.

Python includes a built-in WSGI implementation, so creating a WSGI server is pretty simple. Here's a very minimal example:

from wsgiref.simple_server import make_server

# this will return a text response
def hello_world_app(environ, start_response):
    status = '200 OK'  # HTTP Status
    headers = [('Content-type', 'text/plain')]  # HTTP Headers
    start_response(status, headers)
    return ["Hello World"]

# first argument passed to the function
# is a dictionary containing CGI-style environment variables 
# second argument is a function to call
# make a server and turn it on port 8000
httpd = make_server('', 8000, hello_world_app)
httpd.serve_forever()
Thoc
  • 67
  • 2
  • 12
theodox
  • 12,028
  • 3
  • 23
  • 36
  • Thanks for responding! can you explain how this would look from the Javascript side of things? Assuming the code you wrote above is called "helloWorld.py" and in the command line it takes the argument "hello", how are you passing the script "hello" and receiving the script's response on the JS side of things? – singmotor Apr 01 '17 at 20:55
  • The WSGI server is just listening for http requests and it's return values -- whatever they are -- will be sent in response to requests. This question includes a bunch of examples of different ways to do that in JS: http://stackoverflow.com/questions/247483/http-get-request-in-javascript – theodox Apr 02 '17 at 02:32
  • I appreciate your help, but I'm still having no luck. I've modified the python script to simply create an html page. So literally all I need is to figure out the javascript side of this. I just need to pass the script a string and run it. (the entire script, not just a function in it. i.e.: $ python3 myscript.py inputString – singmotor Apr 03 '17 at 21:22
  • 1
    Well you're making a server that will return the script data as a result (it can be formatted html or just text). The answer I linked above shows how to send a request, if you have to package data you'd put that into the URL of the request like `fetch("http://you/url/here?param=somevalue")` You'd write the server to parse the query string and process it in the results. – theodox Apr 04 '17 at 05:54