Googles App Engine、Python:如何从登录页面获取参数? [英] Googles App Engine, Python: How to get parameters from a log-in pages?

查看:16
本文介绍了Googles App Engine、Python:如何从登录页面获取参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此处的引述:

简而言之……您需要查看登录信息页面,看看它使用了什么参数,例如登录=xxx,密码=yyy,发布到该页面,你将不得不管理饼干也是,那是图书馆的地方像斜纹布等出现在画面中.

So in short ... you need to look into login page, see what params it uses e.g login=xxx, password=yyy, post it to that page and you will have to manage the cookies too, that is where library like twill etc come into picture.

我怎样才能使用 Python 和 Google App Engine 做到这一点?任何人都可以给我一些线索吗?我已经问过一个关于认证请求的问题,但这里似乎问题有所不同,因为这里建议我查看登录页面并获取参数,并且我还必须处理 cookie.

How could I do it using Python and Google App Engine? Can anybody please give me some clue? I have already asked a question about the authenticated request, but here it seems the matter is different as here I am suggested to look into login page and get parameters, and also I have to deal with cookies.

推荐答案

有两种方式

  1. 我告诉过你使用斜纹或机械化,因为斜纹只是机械化的简单包装,你可以只使用机械化(http://wwwsearch.sourceforge.net/mechanize/),但要使用机械化,您可能需要进行一些黑客攻击,请参阅 将机械化模块导入python脚本了解更多详情

  1. 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 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) 查看页面 (https://login.yahoo.com/config/login_verify2?&.src=ym) 并查看表单是什么样的,您可以使用萤火虫进行检查,而不是查看原始 html.

a) look into the page (https://login.yahoo.com/config/login_verify2?&.src=ym) and see what does form look like, you can firebug to inspect instead of looking into raw html.

b) 表单有 login 和 passwd 两个字段,另外还有一些隐藏的字段让我们暂时忽略它们,所以到目前为止我们有表单操作 url="https://login.yahoo.com/config/login?"form_data = {'login' : 'my_login', 'passwd' : 'my_passwd'}

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) 我们可以将上述数据发布到正确的发布网址,它可能会起作用,但通常我们需要转到其他页面,如果我们没有 cookie,它会再次要求登录.所以让我们使用一个 cookie jar,例如

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) 现在来自雅虎的页面,重定向到其他页面,例如如果我想查看邮件页面,我现在将转到该页面,cookie 将负责身份验证,例如

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()

如果您看到打印输出,它会显示xxxx| 注销,嗯……您的浏览器不受官方支持."这意味着它让我登录了 :),但是由于 yahoo 邮件是一个 ajax 页面并且不支持我的简单脚本浏览器,我们可以通过欺骗浏览器类型来绕过这个工具,并且可以做很多事情.

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.

这是最终的代码

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()

您应该查看 mechanzie 代码或类似这样的链接 http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=cookielib_example.py 看看他们是怎么做的.

You should look into mechanzie code or links like this http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=cookielib_example.py to see how they do it.

我们可以发布这些数据

这篇关于Googles App Engine、Python:如何从登录页面获取参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆