If you want to keep the authentication you need to reuse the cookie. I'm not sure if urllib2 is available in python 2.3.4 but here is an example on how to do it:

req1 = urllib2.Request(url1) 
response
= urllib2.urlopen(req1) 
cookie
= response.headers.get('Set-Cookie') 
 
# Use the cookie is subsequent requests 
req2
= urllib2.Request(url2) 
req2
.add_header('cookie', cookie) 
response
= urllib2.urlopen(req2) 

 

-------------------------------------------------------------------------

If this is cookie based authentication use HTTPCookieProcessor:

import cookielib, urllib2 
cj
= cookielib.CookieJar() 
opener
= urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
r
= opener.open("http://example.com/") 

If this is HTTP authentication use basic or digest AuthHandler:

import urllib2 
# Create an OpenerDirector with support for Basic HTTP Authentication... 
auth_handler
= urllib2.HTTPBasicAuthHandler() 
auth_handler
.add_password(realm='PDQ Application', 
                          uri
='https://mahler:8092/site-updates.py', 
                          user
='klem', 
                          passwd
='kadidd!ehopper') 
opener
= urllib2.build_opener(auth_handler) 
# ...and install it globally so it can be used with urlopen. 
urllib2
.install_opener(opener) 
urllib2
.urlopen('http://www.example.com/login.html') 

... and use same opener for every request.



[출처] http://stackoverflow.com/questions/923296/keeping-a-session-in-python-while-making-http-requests

'Hello_World! > 애플추가_파이썬' 카테고리의 다른 글

Python threads - a first example  (0) 2011.12.22
KillApachePy  (0) 2011.11.30
파이썬 v2.7.2 - email: Examples  (0) 2011.10.05
Python - Sending Email using SMTP  (0) 2011.10.05
Gmail의 SMTP를 이용한 메일 발송  (0) 2011.10.05
Posted by bitfox
l