Capture HTML textbox value in python without CGI - python

I need to get the user input in a textbox and process it using my python script when user presses Submit button. Unfortunately I am using an external server that does not support CGI. Is there any other way I can do this? This is my sample HTML.
<body>
<form name="form1" method="post" action="script1.py">
<div id="heading"><big><big><big>My analysis</big></big></big></div>
<textarea cols="50" rows="1" name="Query_text" id="Query_text"></textarea>
<div id="Button"><input name="Submit" value="Submit" type="submit"></div>
</form>
</body>

You can do the form processing with Javascript but hopefully you don't want to save this input beyond the next page the person accesses. Another old school option is emailing yourself the contents of Query_text. But honestly this is a lot more work than you want to do and not worth the effort (for most people). What you need is a new web host.

Related

Cybersource soap toolkit api avoid redirection on 3rd party domain

I am implementing soap toolkit api. After submitting credit card info it redirects me towards the 3rd party page to get the password. I want to avoid that redirection is it possible to load that page within my domain or on my custom page using IFRAME.
Not exactly an answer but more of a request for clarifying information: When you say "it redirects me towards the 3rd party page to get the password", are you talking about the 3D-Secure process? I think I understand what you are trying to accomplish, but in order to use 3D Secure, I think the idea is to guide the end-user through the process of the additional Verification (security) process. So I think the redirection is required as a matter of how the Business Process (or 3D-Secure) works.
It's definitely possible to render it inside an iframe. Assuming you've called ics_pa_enroll to get the pareq and ACS URL you'd do something like this:
<body>
<form action="[ACS URL]" method="POST" target="payerAuthFrame">
<input type="hidden" name="PaReq" value="[Returned PaReq]" />
<input type="hidden" name="MD" value="[Whatever]" />
<input type="hidden" name="TermUrl" value="[Your Site]" />
</form>
<iframe name="payerAuthFrame" width="800" height="800"></iframe>
</body>
and either have the customer submit the form with e.g. a "Proceed" button or else use JavaScript to submit it automatically.

Get the content of an <input> textbox with Python

I'm using a Tornado server running with Python, whose job in convert a .svg file in several font formats. For now I managed to make it generate a fontpack in a .zip file by clicking on a button. My Python code links with html form by using self.request.files['filearg'] where filearg is the name of the file selected in an <input type="file"> box.
But I'd like to make this submit button communicate with a text box in which I'd fill the output format I want my file converted in.
Basically, what I'd want in my html form would look like this:
<form enctype="multipart/form-data" action="/upload" method="post">
File: <input type="file" name="filearg" />
<input type="text" value="" name="format"/>
<input type="submit" value="Generate font(s)" />
</form>
I managed to do it with a Node.js server (with fields.format and so on), but I can't find any way to do this in Python.
I've heard about the CGI forms in HTML, which can permit to get this kind of content. But as if it's not the kind of form I chose, I wonder whether it can be implemented with my actual form.
The Tornado documentation explains how to get the values of form fields, using get_argument:
format = self.get_argument("format")

Using button on a page with python

I am scraping a page for some data, however I need to insert text into a text box, submit the form and scrape the result page. I looked at the page source, but I'm not sure how to activate the button or pass down the argument for it.
Website is http://archive.org/web/web.php
Trying to look at some historicals, and no idea what to use for this. Open to any solution
First you should know that click on that button usually does a POST to some urls, passes the data in that form, here is:
<form id="wwmform" name="wwmform" method="get" action="http://web.archive.org/form-submit.jsp" onsubmit="document.location.href='http://web.archive.org/web/*/'+document.getElementById('wwmurl').value;return false;" style="display:inline;">
<input id="wwmurl" type="text" name="url" size="50" value="http://">
<button type="submit" name="type" value="urlquery" class="roundbox5">Take Me Back</button>
</form>
you see the action attribute? That's where the data goes to.
So in python, you may need urllib and urllib2 to encode the data and post it to the target url and then fetch the outcome.
ps: watch out the onsubmit

Python - posting checkbox to web form

i use this code in python to post something to a form in a web page.
but i dont just want to send some text as an input textbox, i also want to decide if a checkbox in the form is checked or not.
what value do i have to give to the 'checkbox' parameter in the "web_form"?
web_form = [('textbox', text),('checkbox', ?????)]
form_data = urllib.urlencode(web_form)
o = urllib2.build_opener(url)
res = o.open(url, form_data).read()
this is the html of the form:
<form action="?" method="POST" enctype="multipart/form-data">
<textarea name="textbox"></textarea> Checkbox <input type='checkbox' name='cb' > <input type="submit" value="submit" /></div>
</form>
You question is pretty broad since you did not mention with web framework you are using.
Application code could check either for the existence of the checkbox key in the request or check for a particular value...unless you provide further informations: try setting it to something like '1' and see what's happening.

Python mechanize - two buttons of type 'submit'

I have a mechanize script written in python that fills out a web form and is supposed to click on the 'create' button. But there's a problem, the form has two buttons. One for 'add attached file' and one for 'create'. Both are of type 'submit', and the attach button is the first one listed. So when I select the forum and do br.submit(), it clicks on the 'attach' button instead of 'create'. Extensive Googling has yielded nothing useful for selecting a specific button in a form. Does anyone know of any methods for skipping over the first 'submit' button and clicking the second?
I tried using the nr parameter, without any luck.
I was able to get it to work with a combination of the name and label parameters, where "label" seems to correspond to the "value" in the HTML:
Here are my two submit buttons:
<input type="submit" name="Preview" value="Preview" />
<input type="submit" name="Create" value="Create New Page" />
... and here's the code that clicks the first one, goes back, and then clicks the second:
from mechanize import Browser
self.br = Browser()
self.br.open('http://foo.com/path/to/page.html')
self.br.select_form(name='my_form')
self.br['somefieldname'] = 'Foo'
submit_response = self.br.submit(name='Preview', label='Preview')
self.br.back()
self.br.select_form(name='my_form')
self.br['somefieldname'] = 'Bar'
submit_response = self.br.submit(name='Create', label='Create New Page')
There's a variant that also worked for me, where the "name" of the submit button is the same, such as:
<input type="submit" name="action" value="Preview" />
<input type="submit" name="action" value="Save" />
<input type="submit" name="action" value="Cancel" />
and
self.br.select_form(name='my_form')
submit_response = self.br.submit(name='action', label='Preview')
self.br.back()
submit_response = self.br.submit(name='action', label='Save')
IMPORTANT NOTE - I was only able to get any of this multiple-submit-button code to work after cleaning up some HTML in the rest of the page.
Specifically, I could not have <br/> - instead I had to have <br /> ... and, making even less sense, I could not have anything between the two submit buttons.
It frustrated me to no end that the mechanize/ClientForm bug I hunted for over two hours boiled down to this:
<tr><td colspan="2"><br/><input type="submit" name="Preview" value="Preview" /> <input type="submit" name="Create" value="Create New Page" /></td></tr>
(all on one line) did not work, but
<tr><td colspan="2"><br />
<input type="submit" name="Preview" value="Preview" />
<input type="submit" name="Create" value="Create New Page" /></td></tr>
worked fine (on multiple lines, which also shouldn't have mattered).
I like mechanize because it was easy to install (just copy the files into my include directory) and because it's pretty simple to use, but unless I'm missing something major, I think that bugs like this are kind of awful - I can't think of a good reason at all why the first example there should fail and the second should work.
And, incidentally, I also found another mechanize bug where a <textarea> which is contained within a <p> is not recognized as a valid control, but once you take it out of the <p> container it's recognized just fine. And I checked, textarea is allowed to be included in other block-level elements like <p>.
I would suggest you to use Twill which uses mechanize (mostly monkeypatched).
So say you have form with some fields and two submit buttons with names "submit_to_preview" and "real_submit". Following code should work.
BTW remember this is not threadsafe so you might want to use locks in case if you want to use the code in a threaded env.
import twill.commands
b = twill.get_browser()
url = "http://site/myform"
twill.commands.go(url)
twill.commands.fv("2", "name", "Me")
twill.commands.fv("2", "age", "32")
twill.commands.fv("2", "comment", "useful article")
twill.commands.browser.submit("real_submit")
Hope that helps. Cheers.
Use the 'click' method. E.g.
mybrowser.select_form(nr=0)
req = mybrowser.click(type="submit", nr=1)
mybrowser.open(req)
Should work.
I can talk from experience using HTTP, rather than mechanize, but I think this is probably what you want.
When there are two submit buttons in a form, a server can determine which one was pressed, because the client should have added an argument for the submit button. So:
<form action="blah" method="get">
<p>
<input type="submit" name="button_1" value="One" />
<input type="submit" name="button_2" value="Two" />
</p>
</form>
Will take you either the URL:
blah?button_1=One
or:
blah?button_2=Two
Depending on which button was pressed.
If you're programatically determining what arguments are going to be sent, you need to add an argument with the name of the submit button that was pressed, and it's value.

Categories

Resources