[Python-checkins] python/dist/src/Lib xmlrpclib.py,1.20,1.21

effbot@users.sourceforge.net effbot@users.sourceforge.net
2002年10月22日 11:23:02 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv4346/Lib
Modified Files:
	xmlrpclib.py 
Log Message:
add support for basic authentication, based on patch #624180
by Phillip J. Eby
Index: xmlrpclib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xmlrpclib.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** xmlrpclib.py	30 Jun 2002 03:39:14 -0000	1.20
--- xmlrpclib.py	22 Oct 2002 18:23:00 -0000	1.21
***************
*** 44,47 ****
--- 44,48 ----
 # 2002年05月15日 fl Added error constants (from Andrew Kuchling)
 # 2002年06月27日 fl Merged with Python CVS version
+ # 2002年10月22日 fl Added basic authentication (based on code from Phillip Eby)
 #
 # Copyright (c) 1999-2002 by Secret Labs AB.
***************
*** 1045,1048 ****
--- 1046,1080 ----
 
 ##
+ # Get authorization info from host parameter
+ # Host may be a string, or a (host, x509-dict) tuple; if a string,
+ # it is checked for a "user:pw@host" format, and a "Basic
+ # Authentication" header is added if appropriate.
+ #
+ # @param host Host descriptor (URL or (URL, x509 info) tuple).
+ # @return A 3-tuple containing (actual host, extra headers,
+ # x509 info). The header and x509 fields may be None.
+ 
+ def get_host_info(self, host):
+ 
+ x509 = {}
+ if isinstance(host, TupleType):
+ host, x509 = host
+ 
+ import urllib
+ auth, host = urllib.splituser(host)
+ 
+ if auth:
+ import base64
+ auth = base64.encodestring(auth)
+ auth = string.join(string.split(auth), "") # get rid of whitespace
+ extra_headers = [
+ ("Authorization", "Basic " + auth)
+ ]
+ else:
+ extra_headers = None
+ 
+ return host, extra_headers, x509
+ 
+ ##
 # Connect to server.
 #
***************
*** 1053,1056 ****
--- 1085,1089 ----
 # create a HTTP connection object from a host descriptor
 import httplib
+ host, extra_headers, x509 = self.get_host_info(host)
 return httplib.HTTP(host)
 
***************
*** 1072,1076 ****
--- 1105,1115 ----
 
 def send_host(self, connection, host):
+ host, extra_headers, x509 = self.get_host_info(host)
 connection.putheader("Host", host)
+ if extra_headers:
+ if isinstance(extra_headers, DictType):
+ extra_headers = extra_headers.items()
+ for key, value in extra_headers:
+ connection.putheader(key, value)
 
 ##
***************
*** 1148,1167 ****
 # host may be a string, or a (host, x509-dict) tuple
 import httplib
! if isinstance(host, TupleType):
! host, x509 = host
! else:
! x509 = {}
 try:
 HTTPS = httplib.HTTPS
 except AttributeError:
! raise NotImplementedError,\
! "your version of httplib doesn't support HTTPS"
 else:
! return apply(HTTPS, (host, None), x509)
! 
! def send_host(self, connection, host):
! if isinstance(host, TupleType):
! host, x509 = host
! connection.putheader("Host", host)
 
 ##
--- 1187,1199 ----
 # host may be a string, or a (host, x509-dict) tuple
 import httplib
! host, extra_headers, x509 = self.get_host_info(host)
 try:
 HTTPS = httplib.HTTPS
 except AttributeError:
! raise NotImplementedError(
! "your version of httplib doesn't support HTTPS"
! )
 else:
! return apply(HTTPS, (host, None), x509 or {})
 
 ##

AltStyle によって変換されたページ (->オリジナル) /