[英]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.