How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?

后端 未结 9 1424
生来不讨喜
生来不讨喜 2020-12-25 13:34

I\'m a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming

相关标签:
9条回答
  • 2020-12-25 14:19

    When setting this up, please be careful to restrict access to the scripts that take some action on your web server. It is not sufficient to place them in a directory where you just don't publish the URL, because sooner or later somebody will find them.

    At the very least, put these scripts in a location that is password protected. You don't want just anybody out there on the internet being able to run your scripts.

    0 讨论(0)
  • 2020-12-25 14:21

    If you're now using Python 3, print is a function requiring parenthesis.

    #!/Python34/python
    print ("Content-type: text/html\n")
    print ("<html><head>")
    print ("</head><body>")
    print ("Hello World from Python Script.")
    print ("</body></html>")
    
    0 讨论(0)
  • 2020-12-25 14:28

    In an earlier question I asked for a minimal implementation for connecting an AJAX website to Python. This might accomplish what you are looking for. It has a button that triggers some Python code and then shows the result on the page. My solution uses the built-in webserver in Python, so it is really lightweight (no additional packages or even Apache).

    0 讨论(0)
  • 2020-12-25 14:30

    You should look into CherryPy as your HTTP request handler which can be configured to run on top of a very light NGINX web server (or Apache if you prefer).

    You can then either import the other scripts into the service you have running on CherryPy. This would give you a little bit more control for monitoring the process and catching errors if they occur.

    The other option is to simply tell the operating system to trigger the command using os.system(). Check here for an example of different approaches to using os.system().

    0 讨论(0)
  • 2020-12-25 14:34

    This page on the python site has a good description and example of what you need to do to run a python CGI script. Start out with the simplest case first. Just make a short script that prints html.

    #!/usr/bin/python                   #on windows change to your path to the python exe
    
    print "Content-Type: text/html"     # HTML is following
    print                               # blank line, end of headers
    print "<TITLE>CGI script output</TITLE>"
    print "<H1>This is my first CGI script</H1>"
    print "Hello, world!"
    

    When you try this the first time, the hardest part is usually figuring out where to put the script and how to make the web server recognize and run it. If you are using an apache web sever, take a look at these configuration steps.

    Once you have this simple script working, you will just need to add an html form and button tag and use the action property to point it to scripts you want to run.

    0 讨论(0)
  • 2020-12-25 14:36

    All you need is to have it in a directory that is set to execute CGI scripts, then you need to add

    #!/usr/bin/env python
    

    to the top, and it should execute the script as a normal python script if you link to it. (like you would a HTML or PHP file)

    This, of course, assumes your webserver is set up to execute CGI at all, and that you have a linux system.

    There are other, alternative solutions if you want something a bit more fancy, like mod_python and web frameworks like Pylons, but I think the CGI solution would suit your use case better.

    [EDIT]

    On Windows if you have Apache, you SHOULD be able to use the same method, but replacing the stuff behind the #! with the path to your python.exe, like so:

    #!C:\Python25\python.exe -u
    

    The -u is apparently needed to put Python into unbuffered mode according to this link, which also has more indepth info: http://www.imladris.com/Scripts/PythonForWindows.html

    0 讨论(0)
提交回复
热议问题