Python: Value Not Posting To Form


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Python: Value Not Posting To Form
# 1  
Old 07-29-2015
Python: Value Not Posting To Form

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:
Code:
$ ./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:
Code:
#!/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:
Code:
<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:

Code:
        br.form['ctl00$ContentPlaceHolder1$btnAction'] = 'Blacklist Check'
        br.form['ctl00$ContentPlaceHolder1$txtToolInput'] = 'x.x.x.x'

Mods, feel free to mark solved.

Last edited by Azrael; 07-29-2015 at 07:18 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

Remove x lines form top and y lines form bottom using AWK?

How to remove x lines form top and y lines form bottom. This works, but like awk only cat file | head -n-y | awk 'NR>(x-1)' so remove last 3 lines and 5 firstcat file | head -n-3 | awk 'NR>4' (5 Replies)
Discussion started by: Jotne
5 Replies

5. Shell Programming and Scripting

Transpose Data form Different form

HI Guys, I have data in File A.txt RL03 RL03_A_1 RL03_B_1 RL03_C_1 RL03 -119.8 -119.5 -119.5 RL07 RL07_A_1 RL07_B_1 RL07_C_1 RL07 -119.3 -119.5 -119.5 RL15 RL15_A_1 RL15_C_1 RL15 -120.5 -119.4 RL16... (2 Replies)
Discussion started by: asavaliya
2 Replies

6. Programming

libcurl - how to do GET form posting in C?

Hi, I'm learning libcurl so that I can use it in my GTK+/C program and went through the tutorial and code samples but it has only mentioned about POST form as the site I'm using has the GET form and only thing I found googling was using the command in xterm which I don't want. I would be very... (1 Reply)
Discussion started by: jaeezzy
1 Replies

7. UNIX for Dummies Questions & Answers

Posting data to a form using curl

Hello all. I have an incredible number of servers that I need to change a parameter on using a web interface. I'd like to be able to do this via curl, but I'm having some trouble. I filled out the form and hit update while snooping (tcpdump) my interface. That gave the the following as what is... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

8. UNIX for Advanced & Expert Users

Changing Unix form to Microsoft Word form to be able to email it to someone.

Please someone I need information on how to change a Unix form/document into a microsoft word document in order to be emailed to another company. Please help ASAP. Thankyou :confused: (8 Replies)
Discussion started by: Cheraunm
8 Replies
Login or Register to Ask a Question