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

时间:2014-07-16 13:20:23

标签: 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” - &gt;我认为问题出在这里。

2 个答案:

答案 0 :(得分:1)

真正想要做的是使用正确的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()

免责声明:我是电路开发人员。

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

答案 1 :(得分:0)

contenu = string.Template(template).substitute(
            code_name = code_name,
            code_version = code_version,
            install_path = install_path,
            os = user_os,
            user_name = user_login
            server_IP = http:8000/cgi/scriptGet.py
            )

scriptHtml.py

<form name="Data" method="get" action="${server_IP}">
是什么意思?