[Python-checkins] python/dist/src/Lib urllib2.py,1.58,1.59

jhylton at users.sourceforge.net jhylton at users.sourceforge.net
Wed Dec 17 13:52:18 EST 2003


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv16205

Modified Files:
	urllib2.py 
Log Message:
Rewrite AbstractHTTPHandler to use modern httplib interface.

The chief benefit of this change is that requests will now use
HTTP/1.1 instead of HTTP/1.0.  Bump the module version number as part
of the change.

There are two possible incompatibilities that we'll need to watch out
for when we get to an alpha release.  We may get a different class of
exceptions out of httplib, and the do_open() method changed its
signature.  The latter is only important if anyone actually subclasses
AbstractHTTPHandler.


Index: urllib2.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib2.py,v
retrieving revision 1.58
retrieving revision 1.59
diff -C2 -d -r1.58 -r1.59
*** urllib2.py	15 Dec 2003 16:08:48 -0000	1.58
--- urllib2.py	17 Dec 2003 18:52:16 -0000	1.59
***************
*** 121,125 ****
  from urllib import localhost, url2pathname, getproxies
  
! __version__ = "2.1"
  
  _opener = None
--- 121,125 ----
  from urllib import localhost, url2pathname, getproxies
  
! __version__ = "2.4"
  
  _opener = None
***************
*** 208,211 ****
--- 208,213 ----
              return "GET"
  
+     # XXX these helper methods are lame
+ 
      def add_data(self, data):
          self.data = data
***************
*** 937,944 ****
          return request
  
-     # XXX Should rewrite do_open() to use the new httplib interface,
-     # would be a little simpler.
- 
      def do_open(self, http_class, req):
          host = req.get_host()
          if not host:
--- 939,952 ----
          return request
  
      def do_open(self, http_class, req):
+         """Return an addinfourl object for the request, using http_class.
+ 
+         http_class must implement the HTTPConnection API from httplib.
+         The addinfourl return value is a file-like object.  It also
+         has methods and attributes including:
+             - info(): return a mimetools.Message object for the headers
+             - geturl(): return the original request URL
+             - code: HTTP status code
+         """
          host = req.get_host()
          if not host:
***************
*** 948,972 ****
          h.set_debuglevel(self._debuglevel)
  
!         h.putrequest(req.get_method(), req.get_selector())
!         for k, v in req.headers.items():
!             h.putheader(k, v)
!         for k, v in req.unredirected_hdrs.items():
!             h.putheader(k, v)
!         # httplib will attempt to connect() here.  be prepared
!         # to convert a socket error to a URLError.
          try:
!             h.endheaders()
!         except socket.error, err:
              raise URLError(err)
-         if req.has_data():
-             h.send(req.get_data())
  
!         code, msg, hdrs = h.getreply()
!         fp = h.getfile()
!         response = addinfourl(fp, hdrs, req.get_full_url())
!         # XXXX should these be methods, for uniformity with rest of interface?
!         response.code = code
!         response.msg = msg
!         return response
  
  
--- 956,973 ----
          h.set_debuglevel(self._debuglevel)
  
!         headers = dict(req.headers)
!         headers.update(req.unredirected_hdrs)
          try:
!             h.request(req.get_method(), req.get_selector(), req.data, headers)
!             r = h.getresponse()
!         except socket.error, err: # XXX what error?
              raise URLError(err)
  
!         # Pick apart the HTTPResponse object to get the various pieces
!         # of the 
!         resp = addinfourl(r.fp, r.msg, req.get_full_url())
!         resp.code = r.status
!         resp.msg = r.reason
!         return resp
  
  
***************
*** 974,978 ****
  
      def http_open(self, req):
!         return self.do_open(httplib.HTTP, req)
  
      http_request = AbstractHTTPHandler.do_request
--- 975,979 ----
  
      def http_open(self, req):
!         return self.do_open(httplib.HTTPConnection, req)
  
      http_request = AbstractHTTPHandler.do_request
***************
*** 982,986 ****
  
          def https_open(self, req):
!             return self.do_open(httplib.HTTPS, req)
  
          https_request = AbstractHTTPHandler.do_request
--- 983,987 ----
  
          def https_open(self, req):
!             return self.do_open(httplib.HTTPSConnection, req)
  
          https_request = AbstractHTTPHandler.do_request





More information about the Python-checkins mailing list