如何打开.html文件并使用python单击“提交”按钮

时间:2018-03-29 06:57:33

标签: python html python-2.7

我有 .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:

  1. 打开.html文件和
  2. 按“发送数据”按钮
  3. 阅读给出的任何回复(假设响应可能显示在HTML页面或对话框中)

4 个答案:

答案 0 :(得分:1)

发送数据的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>`

答案 1 :(得分:0)

使用flask来托管HTML页面并使用POST请求与python脚本之间发送数据。

此链接可以为您提供更多帮助: https://www.tutorialspoint.com/flask/index.htm

答案 2 :(得分:0)

“单击”按钮只不过是一个POST请求,其中包含正文中的表单数据。

如果你需要通用的东西,你必须解析HTML,找到主机接受的数据并发布它。

但是如果您只是需要这个例子,意思是,您已经知道服务器接受的数据,您可以忘记HTML并使用类似requests的内容来发布数据

答案 3 :(得分:0)

我相信这正是你要找的。它是一个简单的python服务器,带有Python的baseHttpHandler。

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()

您可以通过将所选的适当端口传递给run方法来运行代码,否则将使用默认的80。要测试此内容或进行获取或发布,您可以按如下方式运行卷曲:

发送GET请求:: curl http://localhost

发送HEAD请求:: curl -I http://localhost

发送POST请求:: curl -d“foo = bar&amp; bin = baz”http://localhost

您还可以创建index.html的单独文件,并使用python中的编解码器进行读取。由于输入是字符串,它可能被篡改,最终显示所需的页面。