Myspace auto login using Python

Today I am going to share with you an interesting program - a Python script that logs in to myspace given your username (email address) and password. If you study the script, you will get idea about the following things:
  • HTTP POST in Python
  • How to use cookiejar
  • How to add user-agent, referer etc. to the request header using Python
  • And of course, how to auto login to a site :)
Here is the code:

import urllib2, cookielib, re



url = 'http://secure.myspace.com/index.cfm?fuseaction=login.process'


email = "" #put your email address here
pwd = "" #put your password here


data = "__VIEWSTATE=%2FwEPDwUKLTY1Mjc2MTMwMWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFPWN0bDAwJGN0bDAwJE1haW4kY3BNYWluJFNwbGFzaERpc3BsYXkkY3RsMDAkUmVtZW1iZXJfQ2hlY2tib3gFPWN0bDAwJGN0bDAwJE1haW4kY3BNYWluJFNwbGFzaERpc3BsYXkkY3RsMDAkTG9naW5fSW1hZ2VCdXR0b24%3D&NextPage=&ctl00%24ctl00%24Main%24cpMain%24SplashDisplay%24ctl00%24Email_Textbox="+email+"&ctl00%24ctl00%24Main%24cpMain%24SplashDisplay%24ctl00%24Password_Textbox="+pwd+"&ctl00%24ctl00%24Main%24cpMain%24SplashDisplay%24ctl00%24Login_ImageButton.x=36&ctl00%24ctl00%24Main%24cpMain%24SplashDisplay%24ctl00%24Login_ImageButton.y=15&ctl00%24ctl00%24Main%24cpMain%24SplashDisplay%24ctl00%24nexturl=&ctl00%24ctl00%24Main%24cpMain%24SplashDisplay%24ctl00%24apikey=&ctl00%24ctl00%24Main%24cpMain%24SplashDisplay%24ctl00%24ContainerPage="



cj = cookielib.CookieJar()

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

opener.addheaders = [('Referer', 'http://www.myspace.com/index.cfm?fuseaction=splash'),

('Content-Type', 'application/x-www-form-urlencoded'),

('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')]

usock = opener.open(url, data)

the_page = usock.read()

usock.close()



And check this link to get idea, how do I know details about request header and post values!

Comments

aatiis said…
Hi mate, thanks for the cookiejar example. I myself have been trying to do the same thing (just not with myspace). Your post really helped me.
By the way, have you checked out this year's Google Treasure Hunt? I found the answers using Python. If you're interested, you're welcome to check out my blog post about it :)
Tamim Shahriar said…
Nice to know that it was useful :)
Ben Hammond said…
Hi, i got your script to work to lo in, thanks! Unfortunately I haven't been able to get any further. I don't really understand what firebug has to do with learning this stuff, do you have another site you recommend?

also, can you explain where you got the data field which you send along with the url? thanks!

me@benhammondmusic.com
aatiis said…
I usually get it with Wireshark. How do you get it, subeen?
Tamim Shahriar said…
First I try firebug to get the http requests and responses. If firebug fails, I use wireshark.
admin said…
Thanks for this amazing script! Glad to see some python lovers on here :)
You should have a look to this list of 50 useful python modules.
Keep up the good work!
JOUL said…
Hi there, Subeen,
There is something strange in your script, I don't understand how are the email and password variable being sent to the server. I thought they needed to be encoded and sent with the header or something like that.
Tamim Shahriar said…
When I wrote the script (about 2 months ago), email & password didn't need to be encoded.
thenapking said…
Does this script really work? At no point are the email and pwd variables called.
When i examine the text i get back from the usock.read() it is just the front page of myspace, which could be read w/o logging in.
thenapking said…
sorry, i was being dumb, it's in that very long line starting __VIEWSTATE, which you can't read on this page cos it gets truncated :(
shadyabhi said…
CAn u make a script for logging into and sending sms through mycantos.com
amresh varshney said…
hello plzz explain me the data tag...i need to apply it for way2sms.com..
shadyabhi said…
@amresh

This explains how to make scripts like these

http://linux-junky.blogspot.com/2010/03/python-script-for-sending-free-sms-in.html

Popular posts from this blog

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code

Adjacency Matrix (Graph) in Python