Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pycurl help

Status
Not open for further replies.

krammer

IS-IT--Management
Jul 15, 2007
59
US
I am trying to create a python script that will do a form submission or a "post". I have captured data from wireshark, but I'm not quite sure how to get what I need into python.

Here is the captured data:

Code:
C}bEL[@@}+N&,SP+>\.d
/0POST /somesite/entry.do HTTP/1.1
Host: somesite.network.com
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.1) Gecko/2008071620 Firefox/3.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: [URL unfurl="true"]http://www.somesite.network.com/rubbish.html[/URL]
Content-Type: application/x-[URL unfurl="true"]www-form-urlencoded[/URL]
Content-Length: 298

fvContestId=1242&fvEntryType=STANDARD&fvRId=&fvFirst=First&fvLast=Last&fvEmail=email%40gmail.com&fvEmailConfirm=email%40gmail.com&fvAddress1=100+Fake&fvCity=Somewhere+Somewhere&fvState=NY&fvZip=19000&fvEvePhone=123-123-1234&fvGender=m&submitButton=Enter+for+a+chance+to+win%21

python script:

Code:
#!/usr/bin/env python

import pycurl, StringIO, os

# Constants 
USER_AGENT = 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.1) Gecko/2008071620 Firefox/3.0.1'
LOGIN_URL = '[URL unfurl="true"]http://www.somesite.network.com/rubbish.html'[/URL]
LOGIN_POST_DATA = 'fvContestId=1242&fvEntryType=STANDARD&fvRId=&fvFirst=First&fvLast=Last&fvEmail=email%40gmail.com&fvEmailConfirm=email%40gmail.com&fvAddress1=100+Fake&fvCity=Somewhere+Somewhere&fvState=NY&fvZip=19000&fvEvePhone=123-123-1234&fvGender=m&submitButton=Enter+for+a+chance+to+win%21'
SUBMISSION_URL = '[URL unfurl="true"]http://somesite.network.com//somesite/entry.do'[/URL]
headers = ['Accept-Language: en-us,en;q=0.5', 'Accept-Encoding: gzip,deflate', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Keep-Alive: 300', 'Connection: Keep-Alive']

# Request post page

slurpp = pycurl.Curl()
slurpp.setopt(pycurl.COOKIEFILE, 'cookiejar.txt')
slurpp.setopt(pycurl.URL, SUBMISSION_URL)
slurpp.setopt(pycurl.POSTFIELDS, LOGIN_POST_DATA)
slurpp.setopt(pycurl.POST, 1)
slurpp.setopt(pycurl.VERBOSE, 1)
slurpp.perform()

# Clean up and close out 

slurpp.close()
os.remove('cookiejar.txt')

One part that I know will be a problem, is that the "fvContestID" is always going to be different, so I'm not sure how I would generate the right one for the post.

Greatly appreciate anyone who can help, or get me on the right track. Thanks.
 
Ok so I think I figured out the right data to use for this script based off of the captured wireshark data:

Code:
#!/usr/bin/python

import pycurl, StringIO

# Constants
USER_AGENT = 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.1) Gecko/2008071620 Firefox/3.0.1'
LOGIN_URL = '[URL unfurl="true"]http://www.somesite.network.com/somesite/entry.do'[/URL]
POST_DATA = 'fvContestId=1242&fvEntryType=STANDARD&fvRId=&fvFirst=First&fvLast=Last&fvEmail=email%40gmail.com&fvEmailConfirm=email%40gmail.com&fvAddress1=100+Fake&fvCity=Somewhere+Somewhere&fvState=NY&fvZip=19000&fvEvePhone=123-123-1234&fvGender=m&submitButton=Enter+for+a+chance+to+win%21'
HEADERS = ['Accept-Language: en-us,en;q=0.5', 'Accept-Encoding: gzip,deflate', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Keep-Alive: 300', 'Connection: Keep-Alive']

# Request post page
dev_null = StringIO.StringIO()
pycurlConnect = pycurl.Curl()
pycurlConnect.setopt(pycurl.URL, LOGIN_URL)
pycurlConnect.setopt(pycurl.REFERER, REFERER)
pycurlConnect.setopt(pycurl.POSTFIELDS, POST_DATA)
pycurlConnect.setopt(pycurl.WRITEFUNCTION, dev_null.write)
pycurlConnect.setopt(pycurl.COOKIEFILE, 'cookies.txt')
pycurlConnect.setopt(pycurl.POST, 1)
pycurlConnect.setopt(pycurl.VERBOSE, 1)
pycurlConnect.perform()

# Close connections
dev_null.close()
pycurlConnect.close()

I ran this but I'm not sure if it was correctly submitted. I don't see any errors, but normally if I do this from the website, I will see the page after the submission saying that it was successfully submitted.

How do I allow this page to come through, to verify it was successful?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top