2626>>> import urllib2
2727>>> from keepalive import HTTPHandler
2828>>> keepalive_handler = HTTPHandler()
29- >>> opener = urllib2 .build_opener(keepalive_handler)
30- >>> urllib2 .install_opener(opener)
29+ >>> opener = _urllib.request .build_opener(keepalive_handler)
30+ >>> _urllib.request .install_opener(opener)
3131>>>
32- >>> fo = urllib2 .urlopen('http://www.python.org')
32+ >>> fo = _urllib.request .urlopen('http://www.python.org')
3333
3434If a connection to a given host is requested, and all of the existing
3535connections are still in use, another connection will be opened. If
105105
106106from __future__ import print_function
107107
108- import urllib2
109- import httplib
108+ try :
109+ from thirdparty .six .moves import http_client as _http_client
110+ from thirdparty .six .moves import urllib as _urllib
111+ except ImportError :
112+ from six .moves import http_client as _http_client
113+ from six .moves import urllib as _urllib
114+
110115import socket
111116import thread
112117
@@ -214,7 +219,7 @@ def _remove_connection(self, host, connection, close=0):
214219 def do_open (self , req ):
215220 host = req .host
216221 if not host :
217- raise urllib2 .URLError ('no host given' )
222+ raise _urllib . error .URLError ('no host given' )
218223
219224 try :
220225 h = self ._cm .get_ready_conn (host )
@@ -238,8 +243,8 @@ def do_open(self, req):
238243 self ._cm .add (host , h , 0 )
239244 self ._start_transaction (h , req )
240245 r = h .getresponse ()
241- except (socket .error , httplib .HTTPException ) as err :
242- raise urllib2 .URLError (err )
246+ except (socket .error , _http_client .HTTPException ) as err :
247+ raise _urllib . error .URLError (err )
243248
244249 if DEBUG : DEBUG .info ("STATUS: %s, %s" , r .status , r .reason )
245250
@@ -274,7 +279,7 @@ def _reuse_connection(self, h, req, host):
274279 r = h .getresponse ()
275280 # note: just because we got something back doesn't mean it
276281 # worked. We'll check the version below, too.
277- except (socket .error , httplib .HTTPException ):
282+ except (socket .error , _http_client .HTTPException ):
278283 r = None
279284 except :
280285 # adding this block just in case we've missed
@@ -323,8 +328,8 @@ def _start_transaction(self, h, req):
323328 h .putrequest (req .get_method () or 'GET' , req .selector , skip_host = req .has_header ("Host" ), skip_accept_encoding = req .has_header ("Accept-encoding" ))
324329 else :
325330 h .putrequest (req .get_method () or 'GET' , req .get_selector (), skip_host = req .has_header ("Host" ), skip_accept_encoding = req .has_header ("Accept-encoding" ))
326- except (socket .error , httplib .HTTPException ) as err :
327- raise urllib2 .URLError (err )
331+ except (socket .error , _http_client .HTTPException ) as err :
332+ raise _urllib . error .URLError (err )
328333
329334 if not req .headers .has_key ('Connection' ):
330335 req .headers ['Connection' ] = 'keep-alive'
@@ -341,7 +346,7 @@ def _start_transaction(self, h, req):
341346 def _get_connection (self , host ):
342347 return NotImplementedError
343348
344- class HTTPHandler (KeepAliveHandler , urllib2 .HTTPHandler ):
349+ class HTTPHandler (KeepAliveHandler , _urllib . request .HTTPHandler ):
345350 def __init__ (self ):
346351 KeepAliveHandler .__init__ (self )
347352
@@ -351,7 +356,7 @@ def http_open(self, req):
351356 def _get_connection (self , host ):
352357 return HTTPConnection (host )
353358
354- class HTTPSHandler (KeepAliveHandler , urllib2 .HTTPSHandler ):
359+ class HTTPSHandler (KeepAliveHandler , _urllib . request .HTTPSHandler ):
355360 def __init__ (self , ssl_factory = None ):
356361 KeepAliveHandler .__init__ (self )
357362 if not ssl_factory :
@@ -369,7 +374,7 @@ def _get_connection(self, host):
369374 try : return self ._ssl_factory .get_https_connection (host )
370375 except AttributeError : return HTTPSConnection (host )
371376
372- class HTTPResponse (httplib .HTTPResponse ):
377+ class HTTPResponse (_http_client .HTTPResponse ):
373378 # we need to subclass HTTPResponse in order to
374379 # 1) add readline() and readlines() methods
375380 # 2) add close_connection() methods
@@ -391,9 +396,9 @@ class HTTPResponse(httplib.HTTPResponse):
391396
392397 def __init__ (self , sock , debuglevel = 0 , strict = 0 , method = None ):
393398 if method : # the httplib in python 2.3 uses the method arg
394- httplib .HTTPResponse .__init__ (self , sock , debuglevel , method )
399+ _http_client .HTTPResponse .__init__ (self , sock , debuglevel , method )
395400 else : # 2.2 doesn't
396- httplib .HTTPResponse .__init__ (self , sock , debuglevel )
401+ _http_client .HTTPResponse .__init__ (self , sock , debuglevel )
397402 self .fileno = sock .fileno
398403 self .code = None
399404 self ._method = method
@@ -404,7 +409,7 @@ def __init__(self, sock, debuglevel=0, strict=0, method=None):
404409 self ._url = None # (same)
405410 self ._connection = None # (same)
406411
407- _raw_read = httplib .HTTPResponse .read
412+ _raw_read = _http_client .HTTPResponse .read
408413
409414 def close (self ):
410415 if self .fp :
@@ -468,11 +473,11 @@ def readlines(self, sizehint = 0):
468473 return list
469474
470475
471- class HTTPConnection (httplib .HTTPConnection ):
476+ class HTTPConnection (_http_client .HTTPConnection ):
472477 # use the modified response class
473478 response_class = HTTPResponse
474479
475- class HTTPSConnection (httplib .HTTPSConnection ):
480+ class HTTPSConnection (_http_client .HTTPSConnection ):
476481 response_class = HTTPResponse
477482
478483#########################################################################
@@ -483,14 +488,14 @@ def error_handler(url):
483488 global HANDLE_ERRORS
484489 orig = HANDLE_ERRORS
485490 keepalive_handler = HTTPHandler ()
486- opener = urllib2 .build_opener (keepalive_handler )
487- urllib2 .install_opener (opener )
491+ opener = _urllib . request .build_opener (keepalive_handler )
492+ _urllib . request .install_opener (opener )
488493 pos = {0 : 'off' , 1 : 'on' }
489494 for i in (0 , 1 ):
490495 print (" fancy error handling %s (HANDLE_ERRORS = %i)" % (pos [i ], i ))
491496 HANDLE_ERRORS = i
492497 try :
493- fo = urllib2 .urlopen (url )
498+ fo = _urllib . request .urlopen (url )
494499 foo = fo .read ()
495500 fo .close ()
496501 try : status , reason = fo .status , fo .reason
@@ -510,25 +515,25 @@ def continuity(url):
510515 format = '%25s: %s'
511516
512517 # first fetch the file with the normal http handler
513- opener = urllib2 .build_opener ()
514- urllib2 .install_opener (opener )
515- fo = urllib2 .urlopen (url )
518+ opener = _urllib . request .build_opener ()
519+ _urllib . request .install_opener (opener )
520+ fo = _urllib . request .urlopen (url )
516521 foo = fo .read ()
517522 fo .close ()
518523 m = md5 .new (foo )
519524 print (format % ('normal urllib' , m .hexdigest ()))
520525
521526 # now install the keepalive handler and try again
522- opener = urllib2 .build_opener (HTTPHandler ())
523- urllib2 .install_opener (opener )
527+ opener = _urllib . request .build_opener (HTTPHandler ())
528+ _urllib . request .install_opener (opener )
524529
525- fo = urllib2 .urlopen (url )
530+ fo = _urllib . request .urlopen (url )
526531 foo = fo .read ()
527532 fo .close ()
528533 m = md5 .new (foo )
529534 print (format % ('keepalive read' , m .hexdigest ()))
530535
531- fo = urllib2 .urlopen (url )
536+ fo = _urllib . request .urlopen (url )
532537 foo = ''
533538 while 1 :
534539 f = fo .readline ()
@@ -543,15 +548,15 @@ def comp(N, url):
543548
544549 sys .stdout .write (' first using the normal urllib handlers' )
545550 # first use normal opener
546- opener = urllib2 .build_opener ()
547- urllib2 .install_opener (opener )
551+ opener = _urllib . request .build_opener ()
552+ _urllib . request .install_opener (opener )
548553 t1 = fetch (N , url )
549554 print (' TIME: %.3f s' % t1 )
550555
551556 sys .stdout .write (' now using the keepalive handler ' )
552557 # now install the keepalive handler and try again
553- opener = urllib2 .build_opener (HTTPHandler ())
554- urllib2 .install_opener (opener )
558+ opener = _urllib . request .build_opener (HTTPHandler ())
559+ _urllib . request .install_opener (opener )
555560 t2 = fetch (N , url )
556561 print (' TIME: %.3f s' % t2 )
557562 print (' improvement factor: %.2f' % (t1 / t2 , ))
@@ -562,7 +567,7 @@ def fetch(N, url, delay=0):
562567 starttime = time .time ()
563568 for i in range (N ):
564569 if delay and i > 0 : time .sleep (delay )
565- fo = urllib2 .urlopen (url )
570+ fo = _urllib . request .urlopen (url )
566571 foo = fo .read ()
567572 fo .close ()
568573 lens .append (len (foo ))
@@ -584,7 +589,7 @@ def debug(self, msg, *args): print(msg % args)
584589 info = warning = error = debug
585590 DEBUG = FakeLogger ()
586591 print (" fetching the file to establish a connection" )
587- fo = urllib2 .urlopen (url )
592+ fo = _urllib . request .urlopen (url )
588593 data1 = fo .read ()
589594 fo .close ()
590595
@@ -598,7 +603,7 @@ def debug(self, msg, *args): print(msg % args)
598603 sys .stderr .write ('\r ' )
599604
600605 print (" fetching the file a second time" )
601- fo = urllib2 .urlopen (url )
606+ fo = _urllib . request .urlopen (url )
602607 data2 = fo .read ()
603608 fo .close ()
604609
0 commit comments