0

I have the following details about a POST request

1)The URL: "http://kuexams.org/get_results"
2)Request body: "htno=001111505&ecode=kuBA3_Supply_Dec_2013".

I got this from analyzing HTTP traffic. Then I found this site where you specify these values and it reurns the response. https://requestable.pieterhordijk.com/sSd7o

I need to know how to make something similar to what the site does. Just post the Request body to the URL and return the data for parsing.

P.S: I've tried multiple methods for POST to this site http://kuexams.org/results/3GKZ-D_QBHLWXrg7lZ2IGoKBI7lGfpSK37GNoykJ8k5UerNGYn21FN6w_R5XZ8IQVUHRb8ZYVwq-zN4BhIjusQ,,/ugresults/ug Here is what I tired. It uses a different method but fails miserably.

import mechanize
import cookielib
import urllib
import logging
import sys
import re
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
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)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=3)
br.addheaders = [('User-agent', 'Firefox')]
r = br.open('http://kuexams.org/results/06JZ4SYmTV97s4oROGuLYglPFH3XxJKAunIilJkDBV0gBxSU6YVJ_kXRL0UZb3cIjz9aFdnkYaE-T_S3ubaXPg,,/ugresults/ug')
scrape = r.read()
#print(scrape)
pattern = re.compile('=5&code=(.{5})\'')
img = pattern.findall(scrape) 
print(img)
imgstr = str()
imgstr = img[0]
print(imgstr)
br.select_form("appForm")
br.form["htno"] ='001111441'
br.form["entered_captcha"] = imgstr
response = br.submit()
print response.read()
br.back()
asked Jan 12, 2014 at 3:59
4
  • What's your problem? You didn't ask about it.. Commented Jan 12, 2014 at 4:02
  • @aIKid My below code is not working and also unnecessary. I want to be able to post using the URL and request body like the website I mentioned (which is made by another Stack exchange member for testing POST). Commented Jan 12, 2014 at 4:08
  • What kind of response did you get? Is an exception raised? Commented Jan 12, 2014 at 4:13
  • @aIKid I'm not getting the result. It is just giving me the original site as if nothing has been posted. Besides why go through all this nonsense of searching for capatcha script. I got the direct POST link. It works perfectly when I test it on "requestable.pieterhordijk.com/sSd7o" (This a a site for testing POST and other HTTP requests). But I don't know how to implement this myself. Commented Jan 12, 2014 at 4:18

1 Answer 1

2

I can see post request there not using any cookie or captcha details. Its implemented only client side, So no need for storing cookie or getting captcha therefore this code will work for you. Use requests its cool and easy to use.

import requests
url='http://kuexams.org/get_results'
payload={'htno': '001111441', 'ecode': 'kuBA2_Supply_Dec_2013'}
headers={"User-Agent": "Some Cool Thing"}
r=requests.post(url,headers=headers,data=payload)
print r.content

Note that server is not accepting default User-agent by requests so i added custom one without that it won't accept POST request or might give Forbidden Error.

answered Jan 12, 2014 at 9:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.