I know I'm missing something easy here, but this has been racking my brain. I have a python script I've been trying to use to post to a form. I see the post goes through as I get data back from the page after the post. Only problem is I receive an error the data is not being passed:
$ ./script >> response
$ grep address response
<span id="ctl00_ContentPlaceHolder1_lblNextStep">First we need to get your IP address, then we can tell you if you are blacklisted.</span></h4><br />
Here is the code I have so far with some failed tries in the comments. I have replaced a blacklisted ip with x.x.x.x in this code. You should be able to use any ip to test this with though:
#!/usr/bin/python
import mechanize
from bs4 import BeautifulSoup
import pdb
import cookielib
br=mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# User agent
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
def openPage(url):
br.open( url )
br.response().read()
def blacklisttest():
br.select_form(predicate=lambda f: f.attrs.get('id', None) == 'aspnetForm')
br.form.set_all_readonly(False)
br.form['ctl00$ContentPlaceHolder1$btnAction']
#br.form['ctl00$ContentPlaceHolder1$txtToolInput'] = 'x.x.x.x'
br.form.set_value("x.x.x.x", kind="text", nr=1)
br.submit().read
print br.response().read()
openPage('https://mxtoolbox.com/blacklists.aspx?command=blacklist')
blacklisttest()
Here is what appears to be the relavant code from the page I'm submitting to:
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<h4>Server IP or Domain</h4>
<input name="ctl00$ContentPlaceHolder1$txtToolInput" type="text" id="ctl00_ContentPlaceHolder1_txtToolInput" class="tools_lookup_textbox" />
<input type="submit" name="ctl00$ContentPlaceHolder1$btnAction" value="Blacklist Check" id="ctl00_ContentPlaceHolder1_btnAction" class="btn btn-mx" />
The link to the page is also in the python code if I missed anything. I know a control isn't getting set correctly, but I've tried lots of stuff with no luck. This made me lose a lot of sleep over the weekend. Any help greatly appreciated.
---------- Post updated 07-29-15 at 01:00 AM ---------- Previous update was 07-28-15 at 11:50 AM ----------
Well, I ended up finally figuring this out myself. When I had viewed the site in my browser I did not see the submit button on the next page had a drop down menu that needed to be set. All I needed to change was the following:
br.form['ctl00$ContentPlaceHolder1$btnAction'] = 'Blacklist Check'
br.form['ctl00$ContentPlaceHolder1$txtToolInput'] = 'x.x.x.x'
Mods, feel free to mark solved.