简体   繁体   中英

How to run .py files on server?

How to run a .py files on a browser ? I have a major problem when running python scripts along with html form because the browser was showing the source code on the browser window. How to print output by running html on the server side and not exposing it to the browser on the client side?

I found the answer to my question: Step 1: Name your python files as .cgi instead of .py . .py files only run inside python terminal or if you could edit the server configuration file to show .py files extensions as if they are .cgi files.If you don't have that priviledge you can use .cgi as your file extension to run python on your server and print the output on the client side.

You can use your python code on the server side it is not possible for you to run it on client side so always know what you are doing .cgi gives you that power Step-2: Linking you .html code with the python code you can do that by simply giving the link of your .cgi file into the action attribute of form element.您可以在服务器端使用您的 python 代码,但您无法在客户端运行它,因此始终知道您在做什么.cgi gives you that power Step-2:将您的.html代码与 python 代码链接您可以通过简单地将.cgi文件的链接提供给表单元素的 action 属性来做到这一点。 See Below:-

 <form action="server_action.cgi"> <input name="Message" type="input"> </form>


To print your output in the same html page just use this code要在同一 html 页面中打印输出,只需使用此代码
print('Content-Type:text/html')
print()
print( AFTER THE ABOVE TWO LINES just copy and paste you html code inside this brackets )
function. Just copy and paste the code above TO SEE HOW IT WORKS.

PROCESSING : This step is where python functions play their role to understand what the user is saying in its input and carry output as you wish.处理:这一步是 Python 函数发挥作用的地方,它可以理解用户在输入中所说的内容,并根据需要进行输出。 I have written an example below DO NOT RUN THIS CODE HERE IT WILL NOT PRODUCE OUTPUT BECAUSE PYTHON LIBRARIES ARE NOT Installed ON STACKOVERFLOWS EDITOR.[ SKIP THIS CODE BELOW IF YOU ALREADY HAVE XAMPP INSTALLED AND KNOW HOW TO RUN CGI FILES ON IT ]
Install https://www.apachefriends.org/index.html XAMPP if you do not have a hosted server. Run Python in xampp for windows below is the explanation how to setup xampp for running .cgi files:

  STEP-1:[Download Python]

  Download & install the latest version of python from www.python.org Download 
  Python & click on the windows installer of any version [ex. python-3.6.2]

  STEP 2: [Install Python] Install in any directory of your harddrive [ex. 
  D:\python-3.6.2]
  Click on config button just right to the Start Button and open `httpd.conf` 
  file and make the changes as said in the linked post BELOW  ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇ 
  [RUN PYTHON SCRIPTS WITH XAMPP][1]

Running Python scripts with Xampp
The code for server_action.cgi which i was talking about before the Installation of XAMPP Note that #!C:\\Python39\\python.exe is use at the beginning of your code. Which is the location of your python interpreter (Although it is not needed if your server provides its own libraries to interpret cgi files)

#!C:\Python39\python.exe
import cgi
import cgitb
import cgitb
cgitb.enable()
import cgitb
cgitb.enable(display=0, logdir="/path/to/logdir")
print("<TITLE>CGI script output</TITLE>")
print("<H1>This is my first CGI script</H1>")
print("Hello, world!")
print("""<form action="server_action.cgi">
<input name="Message" type="input">
</form>""")
form = cgi.FieldStorage()
text=form["Message"].value 
#'Message' is the value of my 'name' attribute used in my <input name=""> tag
Use 
print("""
this 
format
""") to print multiple lines in one print output. 


In the above snippet i have used a line which will prevent displaying errors on the browser window of your user.

You can turn it off if you think it will be easy for you to see the errors on the browser window instead of opening another directory.
 import cgitb cgitb.enable(display=0, logdir="/path/to/logdir") This was used to prevent displaying error on the clients browser if any occured. ```/path/to/logdir``` is the path where you want to store the error that was prevented directly on the browser window ```display=0```

Try using django which is a popular python web framework. For more info visit https://www.djangoproject.com/

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