0

I have a .html file where I am sending a value using the submit button as follows:

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

I did go through selenium and from my understanding it doesn't suit me. I would like to have a .html as above and maintain it, so it has to be opened and clicked. A cgi/python example did come into my notice but I would go for it only if there is no other alternative.

How can I use python to:

  1. Open the .html file and
  2. Press the "Send Data" button
  3. Read any response given (assuming the response maybe displayed within a HTML page or a dialog box)

4 Answers 4

2

Python code for sending Data

`def hello():
    Dict={'Title': 'This is title','Subtitle':'subtitle'}
    return render_template('hello.html',Dict=Dict)`

Code for writing values which is passed from python as dictionary into 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>`
Sign up to request clarification or add additional context in comments.

Comments

1

I believe this is exactly what you are looking for .Its a simple python server with the baseHttpHandler of 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()

You can run the code by passing an appropriate port of your choice to run method or the default 80 will be used. To test this or to do a get or post , you could run a curl as follows:

Send a GET request:: curl http://localhost

Send a HEAD request:: curl -I http://localhost

Send a POST request:: curl -d "foo=bar&bin=baz" http://localhost

You could also create a seperate file of index.html and read using the codecs in python. Since the input would be string , it could be tampered with , eventually displaying the desired page.

Comments

0

Use flask to host your HTML Page and use a POST request to send data to and from your python script.

This link should help you more : https://www.tutorialspoint.com/flask/index.htm

2 Comments

I'm sorry but I don't understand "host your HTML page". It'd be great if you could explain. Can't it be simple as opening a page and clicking a button?
I don't want to manually open the file but want the python script to open and click the submit button (or send POST request).
0

"Clicking" a button is nothing more than a POST request with the form data in the body.

If you need something generic, you would have to parse the HTML, find what data the host accepts and POST it.

But if you just need this for this example, meaning, you already know the data the server accepts, you can just forget about the HTML and just use something like requests to post the data

1 Comment

As I understand, requests work with URLs right? I would like to work on the .html file instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.