【问题标题】:Accepting and Sending Cookies with Mechanize使用 Mechanize 接受和发送 Cookie
【发布时间】:2012-11-25 04:13:35
【问题描述】:

我需要在需要 cookie 的网页上填写登录表单并获取有关结果页面的一些信息。由于这需要在晚上非常奇怪的时间完成,我想自动化这个过程,因此我使用机械化(欢迎任何其他建议 - 请注意,我必须在学校服务器上运行我的脚本,我不能安装新软件。Mechanize 是纯 python,所以我能够解决这个问题)。

问题是托管登录表单的页面要求我能够接受和发送 cookie。理想情况下,我希望能够接受和发送服务器发送给我的所有 cookie,而不是硬编码我自己的 cookie。

所以,我开始使用 mechanize 编写脚本,但我似乎处理 cookie 错误。由于我在任何地方都找不到有用的文档(如果我是盲人请指出),我在这里问。

这是我的机械化脚本:

import mechanize as mech

br = mech.Browser()
br.set_handle_robots(False)
print "No Robots"
br.set_handle_redirect(True)
br.open("some internal uOttawa website")
br.select_form(nr=0)
br.form['j_username'] = 'my username'
print "Login: ************"
br.form['j_password'] = 'my password'
print "Password: ************"
response = br.submit()
print response.read()

这会打印以下内容

No Robots
Login: ************
Password: ************

<html>
<body>
    <img src="/idp/images/uottawa-logo-dark.png" />
    <h3>ERROR</h3>
    <p>
        An error occurred while processing your request.  Please contact your helpdesk or
        user ID office for assistance.
    </p>
    <p>
       This service requires cookies.  Please ensure that they are enabled and try your 
       going back to your desired resource and trying to login again.
    </p>
    <p>
       Use of your browser's back button may cause specific errors that can be resolved by
       going back to your desired resource and trying to login again.
    </p>
        <p>
           If you think you were sent here in error,
           please contact technical support
        </p>       
</body>
</html>

如果我在 Chrome 浏览器上禁用 cookie 并尝试相同的操作,这确实是我会得到的页面。

我尝试如下添加一个 cookie jar,但没有成功。

br = mech.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

我查看了多个 mechanize 文档来源。 One of them

A common mistake is to use mechanize.urlopen(), and the .extract_cookies() and 
.add_cookie_header() methods on a cookie object themselves. 
If you use mechanize.urlopen() (or OpenerDirector.open()), 
the module handles extraction and adding of cookies by itself,
so you should not call .extract_cookies() or .add_cookie_header().

这似乎是说我的第一种方法应该有效,但它没有。

如果能提供任何帮助,我将不胜感激 - 这很令人困惑,而且似乎严重缺乏文档。

【问题讨论】:

  • 如果可能的话,我会亲自使用requests 写这个——wwwsearch.sourceforge.net/mechanize/… 有帮助吗? (这似乎暗示传递一个开启者处理程序是必要的)(编辑:现在我在其他地方读到似乎与此相矛盾的......所以我倾向于同意文档!)
  • @inspectorG4dget:你有解决办法吗?我正在寻找答案
  • @John:抱歉,没有。此外,大学将他们的网站更改为对脚本更友好的网站,所以我放弃了这个问题。对不起,我没能帮助你。如果您想要答案,您可以悬赏此问题以吸引其他用户回答。我可能会接受您授予赏金的答案

标签: python cookies mechanize shibboleth


【解决方案1】:

我在使用 Mechanize 对 Shibboleth 网站进行身份验证时遇到了完全相同的消息,只是因为我犯了与您相同的错误。看起来我想通了。

简答

你需要打开的链接是:

br.open("https://web30.uottawa.ca/Shibboleth.sso/Login?target=https://web30.uottawa.ca/hr/web/post-register")

代替:

br.open("https://idp.uottawa.ca/idp/login.jsp?actionUrl=%2Fidp%2FAuthn%2FUserPassword")

为什么?

Shibboleth:通过以下方式轻松安全地连接到各种服务 一个简单的登录。

如果你不告诉他你想登录哪个服务,Shibboleth 登录本身是没有用的。让我们分析 HTTP 标头并比较两个查询获得的 cookie。

1.开业 https://idp.uottawa.ca/idp/login.jsp?actionUrl=%2Fidp%2FAuthn%2FUserPassword

Cookie: JSESSIONID=C2D4A19B2994BFA287A328F71A281C49; _ga=GA1.2.1233451770.1401374115; arp_scroll_position=-1; tools-resize=tools-resize-small; lang-prev-page=en; __utma=251309913.1233451770.1401374115.1401375882.1401375882.1; __utmb=251309913.14.9.1401376471057; __utmz=251309913.1401375882.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); lang=en

2。开业 https://web30.uottawa.ca/Shibboleth.sso/Login?target=https://web30.uottawa.ca/hr/web/post-register

Cookie: JSESSIONID=8D6BEA53823CC1C3045B2CE3B1D61DB0; _idp_authn_lc_key=fc18251e-e5aa-4f77-bb17-5e893d8d3a43; _ga=GA1.2.1233451770.1401374115; arp_scroll_position=-1; tools-resize=tools-resize-small; lang-prev-page=en; __utma=251309913.1233451770.1401374115.1401375882.1401375882.1; __utmb=251309913.16.9.1401378064938; __utmz=251309913.1401375882.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); lang=en

有什么区别?你又多了一个 cookie:_idp_authn_lc_key=1c21128c-2fd7-45d2-adac-df9db4d0a9ad;。我想是cookie说“我想登录there”。

在身份验证过程中,IdP 将设置一个名为 _idp_authn_lc_key。此 cookie 仅包含识别当前身份验证过程所需的信息(通常跨越 多个请求/响应)并在身份验证后删除 过程完成。

来源:https://wiki.shibboleth.net/confluence/display/SHIB2/IdPCookieUsage


我是如何找到that link的?我确实挖掘了网络,发现https://web30.uottawa.ca/hr/web/en/user/registration 使用以下链接重定向到登录表单:

<a href="https://web30.uottawa.ca/Shibboleth.sso/Login?target=https://web30.uottawa.ca/hr/web/post-register" 
   class="button standard"><span>Create your account using infoweb</span></a>

所以这不是机械化的问题,但更多的是 Shibboleth 乍一看有点难以理解。您将找到有关 Shibboleth 身份验证流程here 的更多信息。

【讨论】:

    【解决方案2】:

    您提交表单数据的网站可能需要 CSRF 令牌(您跳过下载的表单中提供的 cookie。)

    尝试使用请求:

    http://docs.python-requests.org/en/latest/user/quickstart/#cookies

    查找 cookie 和/或隐藏的表单字段,然后开火。

    【讨论】:

    • 我使用r = requests.get(url)r.cookies 从页面获取cookie。然后我将mechanize.Browsercookiejar 设置为br.set_cookiejar(r.cookies)。仍然没有运气。想法?
    猜你喜欢
    • 1970-01-01
    • 2020-09-18
    • 2011-04-05
    • 2012-07-21
    • 2017-09-02
    • 2021-05-05
    • 1970-01-01
    • 2010-11-15
    • 2012-04-25
    相关资源
    最近更新 更多