2011-06-21 17 views
1

我處理HTTPS頭,我想HTTP標頭live.comGET HTTPS在Python

import urllib2 

try: 
    email="[email protected]" 
    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信息

如果你問什麼是腳本的更多信息:它是檢查是否電子郵件即時拍攝到的Hotmail使用由該viralbe得到量CheckAvail=

編輯後

感謝您的幫助..修復後只得到Set-Cookie我得到了問題,這是當我得到cookie沒有得到CheckAvil=我得到了很多信息沒有`CheckAvil =在瀏覽器中打開它後,打開源我知道了!看到的畫面enter image description here

+0

對不起,有什麼問題嗎? – senderle

+0

@senderle - 我想問題是如何從'response.info()'對象獲取特定的頭文件。 –

回答

5

通過response.info()返回的對象是mimetools.Message一個實例(如由urllib2 docs描述的),這是rfc822.Message一個子類,其具有getheader()方法。

所以,你可以做到以下幾點:

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

但是,如果你正在檢查郵件,我會建議你,如果可以使用POP3或IMAP(Python帶有模塊兩者)。

+0

Liquid_Fire先生給我通過POP3或IMAP檢查avilabe郵件的想法這看起來好主意 –

+0

查看['poplib']的文檔(http://docs.python.org/library /poplib.html)和['imaplib'](http://docs.python.org/library/imaplib.html)模塊(它們都有接近底部的示例),並查看Windows Live幫助中的POP3或IMAP配置設置將爲您提供服務器,端口和其他需要提供給庫進行連接的設置。 –

+0

傢伙爲什麼'CheckAvail ='不在頭裏!我有perl代碼,並在頭部我得到了'CheckAvail =?!' –

0

這是因爲你的http響應中的'Httponly',這意味着,由於規範,只有http連接才能查看。