473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

using openurl to log into Yahoo services

Hello all,

I'm trying to write a script to log into Yahoo! (mail, groups, etc),
but when I pass the full URL with all form elements via Python, I get
a resutling 400 - Bad Request page. When I just plop the full URL
into a browser, it works perfectly. Is there something I need to
specify in python to submit the correct info (headers, user agent,
etc) to get the response I want? Here is the code I have that returns
the Bad Request:

import urllib, win32gui, win32clipboard, win32con, os, getpass, re
yid = raw_input("Yahoo! ID: ")
pw = getpass.getpass(prompt = 'Yahoo password: ')
url =
"https://login.yahoo.com/config/login?.tries=1&.src=ygrp&.intl=us&.v=0&.challenge= U4VY1YGqdPf8z3SaVccJdhV63YCw&.chkP=Y&.done=http://groups.yahoo.com&login="+yid+"&passwd="+pw+"&.pers istent=y&.save=Sign
In"
temp = urllib.urlopen(url)
grp_list_source = temp.read()

Any thoughts or suggestions? Thanks.


Nov 22 '05 #1
4 3665
On Tue, 15 Nov 2005 16:08:06 -0000,
"joe_public34" <jo**********@yahoo.com> wrote:
Hello all,
I'm trying to write a script to log into Yahoo! (mail, groups, etc),
but when I pass the full URL with all form elements via Python, I get
a resutling 400 - Bad Request page. When I just plop the full URL
into a browser, it works perfectly. Is there something I need to
specify in python to submit the correct info (headers, user agent,
etc) to get the response I want? Here is the code I have that returns
the Bad Request: import urllib, win32gui, win32clipboard, win32con, os, getpass, re
yid = raw_input("Yahoo! ID: ")
pw = getpass.getpass(prompt = 'Yahoo password: ')
url =
"https://login.yahoo.com/config/login?.tries=1&.src=ygrp&.intl=us&.v=0&.challenge= U4VY1YGqdPf8z3SaVccJdhV63YCw&.chkP=Y&.done=http://groups.yahoo.com&login="+yid+"&passwd="+pw+"&.pers istent=y&.save=Sign
In"
temp = urllib.urlopen(url)
grp_list_source = temp.read() Any thoughts or suggestions? Thanks.


It is possible that the "challenge" field is created by yahoo's server
based on the request that caused the server to serve the login page;
perhaps it contains a hash of the user agent that sent the request.
When you reply to that challenge at a much later time, or with a
different user agent, or with something else that's different from the
original request, yahoo's server thinks you're trying to break into
yahoo.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Nov 22 '05 #2
On Tue, 15 Nov 2005 16:08:06 -0000,
"joe_public34" <jo**********@yahoo.com> wrote:
Hello all,
I'm trying to write a script to log into Yahoo! (mail, groups, etc),
but when I pass the full URL with all form elements via Python, I get
a resutling 400 - Bad Request page. When I just plop the full URL
into a browser, it works perfectly. Is there something I need to
specify in python to submit the correct info (headers, user agent,
etc) to get the response I want? Here is the code I have that returns
the Bad Request: import urllib, win32gui, win32clipboard, win32con, os, getpass, re
yid = raw_input("Yahoo! ID: ")
pw = getpass.getpass(prompt = 'Yahoo password: ')
url =
"https://login.yahoo.com/config/login?.tries=1&.src=ygrp&.intl=us&.v=0&.challenge= U4VY1YGqdPf8z3SaVccJdhV63YCw&.chkP=Y&.done=http://groups.yahoo.com&login="+yid+"&passwd="+pw+"&.pers istent=y&.save=Sign
In"
temp = urllib.urlopen(url)
grp_list_source = temp.read() Any thoughts or suggestions? Thanks.


It is possible that the "challenge" field is created by yahoo's server
based on the request that caused the server to serve the login page;
perhaps it contains a hash of the user agent that sent the request.
When you reply to that challenge at a much later time, or with a
different user agent, or with something else that's different from the
original request, yahoo's server thinks you're trying to break into
yahoo.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Nov 22 '05 #3
"joe_public34" <jo**********@yahoo.com> writes:
Hello all,

I'm trying to write a script to log into Yahoo! (mail, groups, etc),


I don't have an answer to your problem but I *do* have a script that
logs into yahoo. It's ugly, but it works. I'm refactoring it - if I
ever get back to it.

I've appended the crucial code, but haven't tried running this
excerpt. If you have problems or questions, feel free to ask.

<mike
myYahooID = 'Your Id Here'

from urllib2 import build_opener, HTTPCookieProcessor
from urllib import urlencode
from BeautifulSoup import BeautifulSoup
from sys import argv, exit

urlopen = build_opener(HTTPCookieProcessor()).open

def login(password):
"""Get past the multiple login forms that Yahoo uses."""

print "Logging in to Yahoo groups."

passage = BeautifulSoup(urlopen('http://groups.yahoo.com').read())
passage.done()
i = 0
while passage and i < 5:
page = passage
passage = handle_login(passage, password)
i = i + 1

if i >= 5:
handle_bogus(page, 'To many login attempts') # Never returns

while 1:
metas = page.fetch('meta', {'http-equiv': 'Refresh'})
if not metas:
return page
# Sigh. It's redirect page that didn't get handled by the library.
page = meta_redirect(metas[0]['content'])

return page
def handle_login(soup, password):
"""Parse a page for login information, and hand back the result of logging in.

Returns None if there is no login form."""

# Get the login page, scrape the form, and log in!
forms = soup.fetch('form', {'name': 'login_form'})
if not forms:
return None
form = forms[0]

postdata = []
for intag in form.fetchNext('input'):
if intag['type'] == 'hidden':
postdata.append((intag['name'], intag['value']))
elif intag['type'] == 'password':
postdata.append((intag['name'], password))
elif intag['type'] == 'checkbox':
postdata.append((intag['name'], 'n'))
elif intag['type'] == 'text' and intag['name'] == 'login':
postdata.append(('login', myYahooID))
elif intag['type'] == 'submit':
if intag.has_key(['name']):
postdata.append((intag['name'], intag['value']))
else:
postdata.append(('submit', intag['value']))
else:
print "Login form had unrecognized input tag:", str(intag)

out = BeautifulSoup(urlopen(form['action'], urlencode(postdata)).read())
out.done()
return out

def meta_redirect(value):
"""Handle a http-equiv redirect, since the library isn't reliable."""

for spam in value.split(';'):
stuff = spam.strip().split('=')
if stuff and stuff[0] == 'url':
out = BeautifulSoup(urlopen(stuff[1]).read())
out.done()
return out
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 22 '05 #4
"joe_public34" <jo**********@yahoo.com> writes:
Hello all,

I'm trying to write a script to log into Yahoo! (mail, groups, etc),


I don't have an answer to your problem but I *do* have a script that
logs into yahoo. It's ugly, but it works. I'm refactoring it - if I
ever get back to it.

I've appended the crucial code, but haven't tried running this
excerpt. If you have problems or questions, feel free to ask.

<mike
myYahooID = 'Your Id Here'

from urllib2 import build_opener, HTTPCookieProcessor
from urllib import urlencode
from BeautifulSoup import BeautifulSoup
from sys import argv, exit

urlopen = build_opener(HTTPCookieProcessor()).open

def login(password):
"""Get past the multiple login forms that Yahoo uses."""

print "Logging in to Yahoo groups."

passage = BeautifulSoup(urlopen('http://groups.yahoo.com').read())
passage.done()
i = 0
while passage and i < 5:
page = passage
passage = handle_login(passage, password)
i = i + 1

if i >= 5:
handle_bogus(page, 'To many login attempts') # Never returns

while 1:
metas = page.fetch('meta', {'http-equiv': 'Refresh'})
if not metas:
return page
# Sigh. It's redirect page that didn't get handled by the library.
page = meta_redirect(metas[0]['content'])

return page
def handle_login(soup, password):
"""Parse a page for login information, and hand back the result of logging in.

Returns None if there is no login form."""

# Get the login page, scrape the form, and log in!
forms = soup.fetch('form', {'name': 'login_form'})
if not forms:
return None
form = forms[0]

postdata = []
for intag in form.fetchNext('input'):
if intag['type'] == 'hidden':
postdata.append((intag['name'], intag['value']))
elif intag['type'] == 'password':
postdata.append((intag['name'], password))
elif intag['type'] == 'checkbox':
postdata.append((intag['name'], 'n'))
elif intag['type'] == 'text' and intag['name'] == 'login':
postdata.append(('login', myYahooID))
elif intag['type'] == 'submit':
if intag.has_key(['name']):
postdata.append((intag['name'], intag['value']))
else:
postdata.append(('submit', intag['value']))
else:
print "Login form had unrecognized input tag:", str(intag)

out = BeautifulSoup(urlopen(form['action'], urlencode(postdata)).read())
out.done()
return out

def meta_redirect(value):
"""Handle a http-equiv redirect, since the library isn't reliable."""

for spam in value.split(';'):
stuff = spam.strip().split('=')
if stuff and stuff[0] == 'url':
out = BeautifulSoup(urlopen(stuff[1]).read())
out.done()
return out
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: asj | last post by:
BIG news from the web services front. Amazon will use web services to tie all its vendors together. The company implementing the system will be using Java/C++ (migrating to all-java later). ...
18
by: smileplzz | last post by:
r there any files which we can download from this group. can we only ask questions in this group? no files stored like it is there in yahoo groups. i think it should be there in group services....
3
by: Matt D | last post by:
In my web service project I've imported an assembly (that I also have the source to) that contains structs that are serializable. I've created some web service methods that return and take as...
3
by: JL | last post by:
I have a VB.NET desktop program that reads/writes data to a server using a Java-based Web Service. This web service, in identical formats, is located on several servers with each server being a...
0
by: joe_public34 | last post by:
Hello all, I'm trying to write a script to log into Yahoo! (mail, groups, etc), but when I pass the full URL with all form elements via Python, I get a resutling 400 - Bad Request page. When I...
3
by: Jim Lewis | last post by:
I have read several things that state accessing a Web Service through a Query String should work. However, when I try to execute http://localhost/webservice1/service1.asmx/HelloWorld I get the...
4
by: archana | last post by:
Hi all, I am new to web services. I am using stateserver to stored data which i set using session. I changed mode in web.config file to StateServer. But didn't change tcipip address. I kept...
8
by: howa | last post by:
Hello, Is it possible to take a screen shot of the browser using PHP? Something similar to Alexa Web Site Screen Dump... thx
8
by: Mo | last post by:
Hi, I can not find a decent example showing how to consume a asp.net 2.0 web service using classic ASP. Does any body have an example I could use? Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.