【问题标题】:get HTTPS header in python在 python 中获取 HTTPS 标头
【发布时间】:2011-06-21 17:50:32
【问题描述】:

我正在处理 HTTPS,我想获取 live.com 的 HTTP 标头

import urllib2

try:
    email="HelloWorld1234560@hotmail.com"
    response = urllib2.urlopen("https://signup.live.com/checkavail.aspx?chkavail="+email+"&tk=1258056184535&ru=http%3a%2f%2fmail.live.com%2f%3frru%3dinbox&wa=wsignin1.0&rpsnv=11&ct=1258055283&rver=6.0.5285.0&wp=MBI&wreply=http:%2F%2Fmail.live.com%2Fdefault.aspx&lc=1036&id=64855&bk=1258055288&rollrs=12&lic=1")
    print 'response headers: "%s"' % response.info()
except IOError, e:
    if hasattr(e, 'code'): # HTTPError
        print 'http error code: ', e.code
    elif hasattr(e, 'reason'): # URLError
        print "can't connect, reason: ", e.reason
    else:
        raise

所以我不想要标题中的所有信息,我只想要 Set-Cookie 信息

如果你问脚本是做什么的:它是通过从这个viralbe CheckAvail=获取数量来检查电子邮件是否可以在hotmail中使用

编辑后

感谢您的帮助 .. 修复后仅获取 Set-Cookie 我遇到了问题,当我获取 cookie 时没有获取 CheckAvil= 在浏览器中打开它并打开源代码后,我得到了很多没有 `CheckAvil= 的信息! !看图

【问题讨论】:

  • @senderle - 我认为问题是如何从response.info() 对象中获取特定的标头。

标签: python https hotmail setcookie


【解决方案1】:

response.info() 返回的对象是mimetools.Message 的实例(如urllib2 docs 所述),它是rfc822.Message 的子类,rfc822.Message 有一个getheader() 方法。

因此您可以执行以下操作:

response = urllib2.urlopen("...")
print response.info().getheader("Set-Cookie") # get the value of the Set-Cookie header

但是,如果您要检查邮件,我建议您使用 POP3 或 IMAP(如果可用)(Python 附带了两者的模块)。

【讨论】:

  • Liquid_Fire 先生告诉我通过POP3IMAP 检查avilabe 邮件的想法,这看起来是个好主意
  • 查看 poplibimaplib 模块的文档(它们都有靠近底部的示例),并查看 Windows Live 帮助中的 POP3 或 IMAP 配置设置,这将提供您需要提供给库以进行连接的服务器、端口和其他设置。
  • 伙计们为什么CheckAvail=不在标题中!我有 perl 的代码,在标题中我得到了CheckAvail= ?!
【解决方案2】:

这是因为你的http响应中有'Httponly',也就是说,由于规范,只有http连接可以查看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多