Python 如何通过按钮检索服务器地址
我在命令行中执行脚本。 当我执行/script.py server_address param2 param3 param4时,它会打开一个带有html表单和按钮的页面,当我们在按钮上键入时,我想检索此服务器地址 这是script.py:Python 如何通过按钮检索服务器地址,python,html,Python,Html,我在命令行中执行脚本。 当我执行/script.py server_address 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_ver
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>
应用程序:${code\u name}
cv:${code_version}
路径安装:${install\u path}
名称:
名字:
邮件:
行动=”http://localhost:8000/cgi/scriptGet.py“->我认为问题就在这里。您真正想做的是使用合适的Python Web框架 CGI十年前就过时了 示例:(使用):
#/usr/bin/env python
“”“表格
一个显示如何处理数据表单的简单示例。
"""
从circuits.web导入服务器、控制器
FORM=”“”
基本表单处理
基本表单处理
使用示例
及其
Web组件来构建一个简单的Web应用程序
一些基本的表单数据。
名字:
姓氏:
scriptHtml.py
<form name="Data" method="get" action="${server_IP}">
是这样吗?在此之前,我们将讨论web框架(它们都非常优秀)您编写的脚本就像服务器使用表单中的四个参数调用它一样。遗憾的是,这不是web的工作方式:您必须从URL获取参数。有一些库可以提供帮助,但我认为,首先您应该更了解HTTP。然后,您将能够更好地理解为什么Python web程序不可用网络最基本的东西之一,也是最常见的协议之一(*HTTP))是请求和响应模型。代理(浏览器)向源服务器发出请求(Apache+mod_wsgi+your_wsgi_callable——一些合适的Python Web服务器/框架).@holdenweb:当我执行脚本时:./script.py param1…param4,它会打开webbrowser并显示网页中的第四个参数(这是我想要的)。@JamesMills:我不想将表单数据发布到文件中,只想在键入一个按钮时恢复服务器地址(例如,当我在按钮上键入时,我们需要访问URL中带有地址服务器的新网页),目前我将localhost作为URL,这并不好。@Glacius很抱歉,如果您不能帮助自己,我们将无法帮助您。我们已尝试过。因此,我想检索服务器地址(我将参数传递给脚本的执行)当我在按钮上键入时(它会打开一个新网页,当我们在按钮上键入时,我要恢复该按钮的服务器地址),我不知道你是否理解我。@Glacius你缺少一些Web开发的重要背景知识和对HTTP的基本理解。如果我们将数据发布到提供表单的Web应用程序中,我几乎没有理由认为表单的提交URI是绝对URI。是的,我是新手,现在我想把server_IP=server_IP在contenu=string.Template(Template.substitute()…中,问题在于action=”“,因为我无法将本地主机放在这里。您只需将http:8000/cgi/scriptGet.py放在服务器\u IP中,我还没有测试您的解决方案,但我认为它不起作用。
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
)
<form name="Data" method="get" action="${server_IP}">