By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,275 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,275 IT Pros & Developers. It's quick & easy.

Question about urllib and posting to an external script

P: 3
I have tried multiple ways of posting information to a website and have failed.

my first attempt was out of the docs:

Expand|Select|Wrap|Line Numbers
  1. import httplib, urllib
  2. params = urllib.urlencode({'email' : 'a@a.com', 'password' : 'pass'})
  3. h = httplib.HTTP("login.myspace.com:80")
  4. h.putrequest("POST", "/index.cfm?fuseaction=login.process&amp")
  5. h.putheader("User-Agent", "Mozilla/5.0 <Windows; U; Windows NT 5.1; en-US; rv;1.8.0.6> Gecko/20060728 Firefox/1.5.0.6")
  6. h.putheader("Content-length", "%d" % len(params))
  7. h.putheader('Accept', 'text/plain')
  8.  
  9. h.endheaders()
  10. h.send(params)
  11. reply, msg, hdrs = h.getreply()
  12. print reply # should be 200
  13. data = h.getfile().read() # get the raw HTML
  14. print data 
this returned a page denying access
and then tried:

Expand|Select|Wrap|Line Numbers
  1. import httplib, urllib
  2. params = urllib.urlencode({'email' : 'ezikz@bonbon.net', 'password' : 'dicksuck'})
  3. data = urllib.urlopen("http://login.myspace.com/index.cfm?fuseaction=login.process&amp:80", params).read() 
  4. print data 
This returned a page that asked me if i wanted to sign up, which confused me even more.

all i want to do is post this form:
[HTML]<form method='POST' enctype='multipart/form-data' action='http://login.myspace.com/index.cfm?fuseaction=login.process&amp'>
<input type="text" name="email" >
<input type="password" name="password" >
<input type="submit" value="Login"/>[/HTML]

Please help i was thinking maybe the site did browser sniffing and only allowed certain types. i dont know.
Sep 4 '06 #1
Share this Question
Share on Google+
2 Replies


P: 1
Get ClientCookie from http://wwwsearch.sourceforge.net/ClientCookie/

Inside the ClientCookie-x.x.x directory, you will find a directory called ClientCookie. Copy it to C:\Python2x\Lib\site-packages.

Change youremail, yourpassword, and anyfriendid in the following code:

Expand|Select|Wrap|Line Numbers
  1. import urllib
  2. import urllib2
  3. import ClientCookie
  4.  
  5. params = urllib.urlencode({'email':'youremail','password':'yourpassword'})
  6. request = urllib2.Request("http://login.myspace.com/index.cfm?fuseaction=login.process&amp",params)
  7. response = urllib2.urlopen(request)
  8. c = ClientCookie.Cookies()
  9. c.extract_cookies(response,request)
  10. request2 = urllib2.Request("http://comment.myspace.com/index.cfm?fuseaction=user.viewComments&friendID=anyfriendid")
  11. c.add_cookie_header(request2)
  12. url2 = urllib2.urlopen(request2)
This loads (as an example) the View All Comments page of the user with anyfriendid--something you must be logged in to do. If you wish to do something else, it should be easy to infer how.

I have tried multiple ways of posting information to a website and have failed.

my first attempt was out of the docs:

Expand|Select|Wrap|Line Numbers
  1. import httplib, urllib
  2. params = urllib.urlencode({'email' : 'a@a.com', 'password' : 'pass'})
  3. h = httplib.HTTP("login.myspace.com:80")
  4. h.putrequest("POST", "/index.cfm?fuseaction=login.process&amp")
  5. h.putheader("User-Agent", "Mozilla/5.0 <Windows; U; Windows NT 5.1; en-US; rv;1.8.0.6> Gecko/20060728 Firefox/1.5.0.6")
  6. h.putheader("Content-length", "%d" % len(params))
  7. h.putheader('Accept', 'text/plain')
  8.  
  9. h.endheaders()
  10. h.send(params)
  11. reply, msg, hdrs = h.getreply()
  12. print reply # should be 200
  13. data = h.getfile().read() # get the raw HTML
  14. print data 
this returned a page denying access
and then tried:

Expand|Select|Wrap|Line Numbers
  1. import httplib, urllib
  2. params = urllib.urlencode({'email' : 'ezikz@bonbon.net', 'password' : 'dicksuck'})
  3. data = urllib.urlopen("http://login.myspace.com/index.cfm?fuseaction=login.process&amp:80", params).read() 
  4. print data 
This returned a page that asked me if i wanted to sign up, which confused me even more.

all i want to do is post this form:
[HTML]<form method='POST' enctype='multipart/form-data' action='http://login.myspace.com/index.cfm?fuseaction=login.process&amp'>
<input type="text" name="email" >
<input type="password" name="password" >
<input type="submit" value="Login"/>[/HTML]

Please help i was thinking maybe the site did browser sniffing and only allowed certain types. i dont know.
Jan 7 '07 #2

bartonc
Expert 5K+
P: 6,596
Get ClientCookie from http://wwwsearch.sourceforge.net/ClientCookie/

Inside the ClientCookie-x.x.x directory, you will find a directory called ClientCookie. Copy it to C:\Python2x\Lib\site-packages.

Change youremail, yourpassword, and anyfriendid in the following code:

Expand|Select|Wrap|Line Numbers
  1. import urllib
  2. import urllib2
  3. import ClientCookie
  4.  
  5. params = urllib.urlencode({'email':'youremail','password':'yourpassword'})
  6. request = urllib2.Request("http://login.myspace.com/index.cfm?fuseaction=login.process&amp",params)
  7. response = urllib2.urlopen(request)
  8. c = ClientCookie.Cookies()
  9. c.extract_cookies(response,request)
  10. request2 = urllib2.Request("http://comment.myspace.com/index.cfm?fuseaction=user.viewComments&friendID=anyfriendid")
  11. c.add_cookie_header(request2)
  12. url2 = urllib2.urlopen(request2)
This loads (as an example) the View All Comments page of the user with anyfriendid--something you must be logged in to do. If you wish to do something else, it should be easy to infer how.
There is great info here for our members. Thank you for taking the time to post. I hope that you stick around, ask some, answer some, or just post some code that you are working on.
Jan 8 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.