There are two ways
AS I told you use twill or mechanize, as twill is just a simple wrapper over mechanize you may just use mechanize(http://wwwsearch.sourceforge.net/mechanize/), but to use mechanize you may need to do some hacking see http://stackoverflow.com/questions/275980/import-mechanize-module-to-python-script for more details
Do it the hard way and learn something while doing that
Lets see how to login to yahoo
a) look into the page (https://login.yahoo.com/config/login%5Fverify2?&.src=ym) and see what does form look like, you can firebug to inspect instead of looking into raw html.
b) form has login and passwd two field, Plus some more hidden fields lets ignore them for now, so till now we have
form action url= "https://login.yahoo.com/config/login?"
form_data = {'login' : 'my_login', 'passwd' : 'my_passwd'}
c) we can post above data to the correct post url, and it may work but usually we will need to go to other pages and if we do not have cookie it will ask again for login. so lets use a cookie jar e.g.
jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
form_data = urllib.urlencode(form_data)
# data returned from this pages contains redirection
resp = opener.open(url, form_data)
d) now the page from yahoo, redirects to other pages, e.g. if I want to see mail page, i will now go to that and cookies will take care of authentication e.g.
resp = opener.open('http://mail.yahoo.com')
print resp.read()
If you see printout it says , "xxxx| logout , Hmm... your browser is not officially supported." that means it has logged me in :), but as yahoo mail is a ajax page and doesn't support my simple scripting browser, we can get past this tool by spoofing browser type, and can do lots of stuff.
Here is the final code
import urllib, urllib2, cookielib
url = "https://login.yahoo.com/config/login?"
form_data = {'login' : 'your-login', 'passwd' : 'your-pass'}
jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
form_data = urllib.urlencode(form_data)
# data returned from this pages contains redirection
resp = opener.open(url, form_data)
# yahoo redirects to http://my.yahoo.com, so lets go there insetad
resp = opener.open('http://mail.yahoo.com')
print resp.read()
You should look into mechanzie code or links like this http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=cookielib%5Fexample.py to see how they do it.
we can post this data