2011-07-02 42 views
9

Cố gắng để có được một kịch bản đăng nhập làm việc, tôi tiếp tục nhận được cùng một trang đăng nhập trở lại, vì vậy tôi bật gỡ lỗi của dòng http (không thể sử dụng wireshark hoặc thích vì https).gỡ lỗi kết nối với urllib2 + httplib.debuglevel đôi khi không hiển thị thông tin gỡ lỗi

Tôi không có gì, vì vậy tôi đã sao chép ví dụ, nó hoạt động. Bất kỳ truy vấn nào đến google.com đều hoạt động, nhưng với trang đích của tôi không hiển thị gỡ lỗi, sự khác biệt là gì? Nếu đó là chuyển hướng tôi mong đợi sẽ thấy tiêu đề nhận/chuyển hướng đầu tiên và chuyển hướng http://google.

import urllib 
import urllib2 
import pdb 

h=urllib2.HTTPHandler(debuglevel=1) 
opener = urllib2.build_opener(h) 
urllib2.install_opener(opener) 
print '================================' 
data = urllib2.urlopen('http://google.com').read() 
print '================================' 
data = urllib2.urlopen('https://google.com').read() 
print '================================' 
data = urllib2.urlopen('https://members.poolplayers.com/default.aspx').read() 
print '================================' 
data = urllib2.urlopen('https://google.com').read() 

Khi tôi chạy, tôi nhận được thông báo này.

$ python ex.py 
================================ 
send: 'GET/HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: google.com\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n' 
reply: 'HTTP/1.1 301 Moved Permanently\r\n' 
header: Location: http://www.google.com/ 
header: Content-Type: text/html; charset=UTF-8 
header: Date: Sat, 02 Jul 2011 16:20:11 GMT 
header: Expires: Mon, 01 Aug 2011 16:20:11 GMT 
header: Cache-Control: public, max-age=2592000 
header: Server: gws 
header: Content-Length: 219 
header: X-XSS-Protection: 1; mode=block 
header: Connection: close 
send: 'GET/HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.google.com\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n' 
reply: 'HTTP/1.1 200 OK\r\n' 
header: Date: Sat, 02 Jul 2011 16:20:12 GMT 
header: Expires: -1 
header: Cache-Control: private, max-age=0 
header: Content-Type: text/html; charset=ISO-8859-1 
header: Set-Cookie: PREF=ID=4ca9123c4f8b617f:FF=0:TM=1309623612:LM=1309623612:S=o3GqHRj5_3BkKFuJ; expires=Mon, 01-Jul-2013 16:20:12 GMT; path=/; domain=.google.com 
header: Set-Cookie: NID=48=eZdXW-qQQC2fRrXps3HpzkGgeWbMCnyT_taxzdvW1icXS1KSM0SSYOL7B8-OPsw0eLLAbvCW863Viv9ICDj4VAL7dmHtF-gsPfro67IFN5SP6WyHHpLL7JsS_-MOvwSD; expires=Sun, 01-Jan-2012 16:20:12 GMT; path=/; domain=.google.com; HttpOnly 
header: Server: gws 
header: X-XSS-Protection: 1; mode=block 
header: Connection: close 
================================ 
send: 'GET/HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.google.com\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n' 
reply: 'HTTP/1.1 200 OK\r\n' 
header: Date: Sat, 02 Jul 2011 16:20:14 GMT 
header: Expires: -1 
header: Cache-Control: private, max-age=0 
header: Content-Type: text/html; charset=ISO-8859-1 
header: Set-Cookie: PREF=ID=d613768b3704482b:FF=0:TM=1309623614:LM=1309623614:S=xLxMwBVKEG_bb1bo; expires=Mon, 01-Jul-2013 16:20:14 GMT; path=/; domain=.google.com 
header: Set-Cookie: NID=48=im_KcHyhG2LrrGgLsQjYlwI93lFZa2jZjEYBzdn-xXEyQnoGo8xkP0234fROYV5DScfY_6UbbCJFtyP_V00Ji11kjZwJzR63LfkLoTlEqiaY7FQCIky_8hA2NEqcXwJe; expires=Sun, 01-Jan-2012 16:20:14 GMT; path=/; domain=.google.com; HttpOnly 
header: Server: gws 
header: X-XSS-Protection: 1; mode=block 
header: Connection: close 
================================ 
================================ 
send: 'GET/HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.google.com\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n' 
reply: 'HTTP/1.1 200 OK\r\n' 
header: Date: Sat, 02 Jul 2011 16:20:16 GMT 
header: Expires: -1 
header: Cache-Control: private, max-age=0 
header: Content-Type: text/html; charset=ISO-8859-1 
header: Set-Cookie: PREF=ID=dc2cb55e6476c555:FF=0:TM=1309623616:LM=1309623616:S=o__g-Zcpts392D9_; expires=Mon, 01-Jul-2013 16:20:16 GMT; path=/; domain=.google.com 
header: Set-Cookie: NID=48=R5gy1aTMjL8pghxQmfUkJaMLc3SxmpFxu5XpoZELAsZrdf8ogQLwyo9Vbk_pRkoETvKE-beWbHHBZu3xgJDt6IsjwmSHPaMGSzxXvsWERxsbKwQMy-wlLSfasvUq5x6q; expires=Sun, 01-Jan-2012 16:20:16 GMT; path=/; domain=.google.com; HttpOnly 
header: Server: gws 
header: X-XSS-Protection: 1; mode=block 
header: Connection: close 

Trả lời

10

Bạn sẽ cần một HTTP S Handler:

h = urllib2.HTTPSHandler(debuglevel=1) 
Các vấn đề liên quan