[英]How to open a .html file and click submit button using python
我有一个.html文件,其中使用“提交”按钮发送值,如下所示:
<HTML>
<HEAD>
<TITLE>XYZ Ltd.</TITLE>
</HEAD>
<BODY>
<FORM ACTION="http://192.168.2.2/cgi-bin/http_recv.cgi" METHOD="POST">
<TEXTAREA NAME="DATA_SEND" COLS='160' ROWS='40' WRAP='none'>
</TEXTAREA>
<INPUT TYPE="SUBMIT" VALUE="Send Data">
</FORM>
</BODY>
</HTML>
我确实检查过硒,但根据我的理解,它不适合我。 我想要一个.html并进行维护,因此必须打开并单击它。 我确实注意到了一个cgi / python示例,但是只有在没有其他选择的情况下,我才会去使用它。
如何使用python执行以下操作:
用于发送数据的Python代码
`def hello():
Dict={'Title': 'This is title','Subtitle':'subtitle'}
return render_template('hello.html',Dict=Dict)`
用于将值从python作为字典传递到HTML的编写值的代码
`<form accept-charset="utf-8" class="simform" method="POST"
enctype=multipart/form-data>
Title <input type="text" name="Title" value="{{ Dict.get('Title')
}}" maxlength="36">
SubTitle <input type="text" name="SubTitle" value="{{
Dict.get('SubTitle') }}" maxlength="70">
<button type="submit" class="save btn btn-default">Submit</button>
</form>`
使用flask来承载HTML页面,并使用POST请求与python脚本之间来回发送数据。
该链接应该为您提供更多帮助: https : //www.tutorialspoint.com/flask/index.htm
“单击”按钮只不过是在正文中包含表单数据的POST请求。
如果需要通用的东西,则必须解析HTML,找到主机接受的数据并将其发布。
但是,如果您仅需要此示例,即意味着您已经知道服务器接受的数据,则可以不用考虑HTML,而可以使用诸如请求之类的方式发布数据
我相信这正是您要寻找的东西。它是带有Python的baseHttpHandler的简单python服务器。
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write("<html><body><h1>hi!</h1></body></html>")
def do_HEAD(self):
self._set_headers()
def do_POST(self):
# Doesn't do anything with posted data
self._set_headers()
self.wfile.write("<html><body><h1>POST!</h1></body></html>")
def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...'
httpd.serve_forever()
您可以通过传递您选择的适当端口来运行方法来运行代码,否则将使用默认的80。 要对此进行测试或进行get或post,可以按如下所示运行curl:
发送GET请求:: curl http:// localhost
发送HEAD请求:: curl -I http:// localhost
发送POST请求:: curl -d“ foo = bar&bin = baz” http:// localhost
您还可以创建一个单独的index.html文件,并使用python中的编解码器进行读取。 由于输入将是string,因此可能会对其进行篡改,最终显示所需的页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.