python - 如何通过按钮检索服务器地址

标签 python html

我在命令行中执行我的脚本。 当我执行 ./script.py server_adress param2 param3 param4 它打开一个带有 html 表单和按钮的页面,当我们在按钮上键入时,我想检索这个服务器地址。

这是 script.py 代码的一部分:

import os, sys, platform, getpass, tempfile
import webbrowser
import string
import json


def main( server_IP, code_name, code_version, install_path):

  template = open('scriptHmtl.phtml').read()

  contenu = string.Template(template).substitute(
            code_name = code_name,
            code_version = code_version,
            install_path = install_path,
            os = user_os,
            user_name = user_login
            )
f = tempfile.NamedTemporaryFile(prefix='/tmp/info.html', mode='w', delete=False)
f.write(contenu)
f.close()

webbrowser.open(f.name)

if __name__ == "__main__":
  server_IP = sys.argv[1]
  code_name = sys.argv[2]
  code_version = sys.argv[3]
  install_path = sys.argv[4]

  user_os = sys.platform
  sys.argv.append(user_os)

  user_login = getpass.getuser()
  sys.argv.append(user_login)

  config_file = open("config.txt", "w")
  json.dump(sys.argv, config_file)

  main(server_IP, code_name, code_version, install_path)

这里是获取地址的html代码,scriptHtml.py

<html>
<body>
App: ${code_name}<br/><br/>

cv: ${code_version}<br/><br/>

path install: ${install_path}<br/><br/>

<form name="Data" method="get" action="http://localhost:8000/cgi/scriptGet.py">
    Name: <input type="text" name="name"><br/><br/>
    First name: <input type="text" name="fn"/><br/><br/>
    Mail: <input type="text" name="mail"/><br/><br/>
    <input type="submit" value="OK"/>
</form>
</body>
</html>

action="http://localhost:8000/cgi/scriptGet.py"-> 我认为问题出在这里。

最佳答案

在这里您真正想要做的是使用合适的 Python Web 框架。

CGI 几十年前就不再流行了?

示例:(使用 circuits ):

#!/usr/bin/env python

"""Forms

A simple example showing how to deal with data forms.
"""

from circuits.web import Server, Controller


FORM = """
<html>
 <head>
  <title>Basic Form Handling</title>
 </head>
 <body>
  <h1>Basic Form Handling</h1>
  <p>
   Example of using
   <a href="http://circuitsframework.com/">circuits</a> and its
   <b>Web Components</b> to build a simple web application that handles
   some basic form data.
  </p>
  <form action="/save" method="POST">
   <table border="0" rules="none">
    <tr>
     <td>First Name:</td>
     <td><input type="text" name="firstName"></td>
    </tr>
    <tr>
     <td>Last Name:</td>
     <td><input type="text" name="lastName"></td>
    </tr>
     <tr>
      <td colspan=2">
       <input type="submit" value="Save">
     </td>
     </tr>
   </table>
  </form>
 </body>
</html>"""


class Root(Controller):

    def index(self):
        """Request Handler

        Our index request handler which simply returns a response containing
        the contents of our form to display.
        """

        return FORM

    def save(self, firstName, lastName):
        """Save Request Handler

        Our /save request handler (which our form above points to).
        This handler accepts the same arguments as the fields in the
        form either as positional arguments or keyword arguments.

        We will use the date to pretend we've saved the data and
        tell the user what was saved.
        """

        return "Data Saved. firstName={0:s} lastName={1:s}".format(
            firstName, lastName
        )


app = Server(("0.0.0.0", 8000))
Root().register(app)
app.run()

免责声明:我是电路的开发者。

注意:还有许多其他优秀的 Python Web 框架:

关于python - 如何通过按钮检索服务器地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781917/

相关文章:

python - 如何根据三列中的顺序将三列合并为一列

python - QTableView拦截Key_Down和Key_Up事件

javascript - 如何提升两个弹出窗口

javascript - 无法在 div block 中呈现 Bootstrap Glyphicons

javascript - jquery 带有类别的复选框过滤器

python - 重写字典理解

python - 字典强制有意还是无意?

python - 扭曲的循环调用不调用 errback

html - HTML 文件中的 &lt;style&gt; 标签未显示在浏览器中

javascript - 无法将 jquery 元素附加到新包装的元素