[Tutor] urllib2 & form submit

Kent Johnson kent37 at tds.net
Thu Oct 27 00:52:04 CEST 2005


Ed Hotchkiss wrote:
> i am trying to write a class to log into myspace. either i am doing 
> something wrong, or... myspace is requiring that i accept a cookie, or 
> create a session? however i am new to urllib and urllib2.

It's very likely that myspace is returning a cookie from the login, this is a common way to handle login sessions. You have to install an HTTPCookieProcessor into urllib2. In your setup code before you call urllib2.urlopen(), put these two lines:
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) 
    urllib2.install_opener(opener)

Now when you use urllib2.urlopen() it will save any cookies returned by the server, and send them in future requests.

There is an article on cookies at www.voidspace.org.uk. The site seems to be down now but the Google cache version is here:
http://64.233.161.104/search?q=cache:24L65pecYJ0J:www.voidspace.org.uk/python/articles/cookielib.shtml+httpcookieprocessor&hl=en&client=firefox-a

The article is overly complicated because it shows how to use an older cookie library as well as urllib2. You can just focus on the urllib2 part.

Kent

>  
> heres my code:
> correctly formatted code is at: http://www.jeah.net/~marla/myspace.py
>  
> #!/bin/env python
> 
> # -----------
> 
> import sgmllib
> import urllib
> import urllib2
> import urlparse
> 
> # -----------
> 
> class myspace:
> 
> # login to one myspace account
> def login(self, user, password):
> the_url = 
> "http://viewmorepics.myspace.com/index.cfm?fuseaction=login.process"
> user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
> headers = { 'User-Agent' : user_agent }
> values = { 'email' : user,
>      'password' : password }
> data = urllib.urlencode(values)
> req = urllib2.Request(the_url, data)
> try:
> handle = urllib2.urlopen (req)
> except IOError:
> print 'Something went wrong'
> else:
> x = handle.read()
> temp = open("edspage.html", "w")
> temp.write(x)
> temp.close()
> 
> # add friend based on users URL
> def addFriend(self, friendURL):
> return 1
> 
> # delete friend based on users URL
> def deleteFriend(self, friendURL):
> return 1
> 
> # post a friends bulletin
> def postBulletin(self, bulletin):
> return 1
> 
> # read all bulletins into a tuple named 'bulletins'
> def readBulletins(self):
> bulletins = {}
> 
> # a photo to your profile
> def addPhoto(self, photoLocation):
> return 1
> 
> # delete a photo from your profile
> def deletePhoto(self, photoID):
> return 1
> 
> # return a tuple of people that match your browsing criteria
> def browse(self, sex, ageBegin, ageEnd, gayBiStraight, zipCode, zipRange):
>             return 1
> 
> # stick someones myspace page into an html file
> def grabUserPageHTML(self, xURL, outputFile):
> req = urllib2.Request(xURL)
> handle = urllib2.urlopen(req)
> thepage = handle.read()
>                 temp = open(outputFile, "w")
>                 temp.write (thepage)
>                 temp.close()
> 
> def logout(self):
> return 1
> 
> # -----------
> 
> # example of class usage
> 
> me = myspace()
> me.login("xxx at gmail.com <mailto:xxx at gmail.com>", "password here")
> #me.grabUserPageHTML("http://www.myspace.com/blahuser/","blah.html 
> <http://www.myspace.com/blahuser/","blah.html>")
> #me.logout()
> 
> # -----------
> EOF
>  
>  
> the form uses POST method. the action for the form is the_url. what am i 
> doing wrong? all that the script does, is to refer me to another login 
> page. is it detecting somehow that i am a script and not a users 
> browser? any help would be appreciate, lost in the water here.
> 
> -- 
> edward hotchkiss
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list