简体   繁体   English

如何在我的服务器上运行python脚本?

[英]How to run python script on my server?

I have a server hosting by Blueshot. 我有一个由Blueshot托管的服务器。 I have been using php to get post variables from iPhone... But now I have to use python to get post variable from iPhone. 我一直在使用php从iPhone获取发布变量......但是现在我必须使用python从iPhone获取post变量。 I wrote something like this 我写了这样的东西

import cgi
import cgitb; cgitb.enable()  # for troubleshooting

print "Content-type: text/html"
print

print """
<html>

<head><title>Sample CGI Script</title></head>

<body>

  <h3> Sample CGI Script </h3>
"""

form = cgi.FieldStorage()
message = form.getvalue("message", "(no message)")

print """

  <p>Previous message: %s</p>

  <p>form

  <form method="post" action="index.cgi">
    <p>message: <input type="text" name="message"/></p>
  </form>

</body>

</html>
""" % message

And uploaded to my server but it doesn't work...If I navigate to the page then it just shows the source code.. I don't know whether or not python is installed on my server (I believe python might be installed as default)..How do I check if python is runnable on my server and if not how can I run python scripts on my server? 并上传到我的服务器,但它不起作用...如果我导航到页面然后它只显示源代码..我不知道我的服务器上是否安装了python(我相信python可能已安装默认情况下)..如何检查python是否可以在我的服务器上运行,如果不能,我怎么能在我的服务器上运行python脚本? All I want to do is to get POST variables from iPhone (I know how to send the variables from iPhone) Thanks in advance... 我想做的就是从iPhone获取POST变量(我知道如何从iPhone发送变量)在此先感谢...

Here's a small checklist of things to check when CGI scripts aren't working: 以下是CGI脚本不工作时需要检查的小清单:

  • Is it in a cgi-bin (or equivalent) folder? 它是在cgi-bin (或等效的)文件夹中吗?
  • Is it executable? 它可执行吗?
  • Is it readable? 它可读吗?
  • Does it have a hashbang? 它有一个hashbang吗?
  • Is your server set up to process CGI scripts? 您的服务器是否设置为处理CGI脚本?

Here's a sample CGI script you can use for testing: 这是一个可用于测试的示例CGI脚本:

#!/usr/bin/env python
import cgi; cgi.test()

Name it test.py , put it somewhere in cgi-bin , and make sure it's executable: 将其命名为test.py ,将其放在cgi-bin某个位置,并确保它是可执行的:

$ chmod a+rx test.py

Some web servers only recognize CGI scripts with certain extensions, so if .py doesn't work, you may want to try .cgi . 某些Web服务器只识别具有某些扩展名的CGI脚本,因此如果.py不起作用,您可能需要尝试.cgi

This is a webserver configuration problem not a programming issue. 这是一个Web服务器配置问题,而不是编程问题。

If you're running Apache, this might work: 如果您正在运行Apache,这可能会起作用:


chmod 755 myscript.py
ln -s myscript.py myscript.cgi

Then put this in your .htaccess file: 然后把它放在.htaccess文件中:


AddHandler cgi-script .cgi

If you can't run the ln command because of no shell access, just upload with the .cgi extension to begin with. 如果由于没有shell访问权限而无法运行ln命令,则只需使用.cgi扩展名上传即可。 Or, as icktoofay said, put it in your cgi-bin folder, then you won't need the .htaccess modification either. 或者,正如icktoofay所说,将它放在你的cgi-bin文件夹中,那么你也不需要.htaccess修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM