1
- #
2
- # HTTP/1.1 client library
3
- #
4
-
5
- # ### this may as well go into a doc string...
6
1
"""HTTP/1.1 client library
7
2
8
3
<intro stuff goes here>
71
66
Req-sent-unread-response _CS_REQ_SENT <response_class>
72
67
"""
73
68
74
-
75
69
import socket
76
70
import string
77
71
import mimetools
@@ -599,14 +593,16 @@ def connect(self):
599
593
self .sock = FakeSocket (sock , ssl )
600
594
601
595
602
- class HTTP ( HTTPConnection ) :
596
+ class HTTP :
603
597
"Compatibility class with httplib.py from 1.5."
604
598
605
599
_http_vsn = 10
606
600
_http_vsn_str = 'HTTP/1.0'
607
601
608
602
debuglevel = 0
609
603
604
+ _connection_class = HTTPConnection
605
+
610
606
def __init__ (self , host = '' , port = None , ** x509 ):
611
607
"Provide a default host, since the superclass requires one."
612
608
@@ -617,7 +613,11 @@ def __init__(self, host='', port=None, **x509):
617
613
# Note that we may pass an empty string as the host; this will throw
618
614
# an error when we attempt to connect. Presumably, the client code
619
615
# will call connect before then, with a proper host.
620
- HTTPConnection .__init__ (self , host , port )
616
+ self ._conn = self ._connection_class (host , port )
617
+ # set up delegation to flesh out interface
618
+ self .send = self ._conn .send
619
+ self .putrequest = self ._conn .putrequest
620
+ self .endheaders = self ._conn .endheaders
621
621
622
622
# we never actually use these for anything, but we keep them here for
623
623
# compatibility with post-1.5.2 CVS.
@@ -630,8 +630,8 @@ def connect(self, host=None, port=None):
630
630
"Accept arguments to set the host/port, since the superclass doesn't."
631
631
632
632
if host is not None :
633
- self ._set_hostport (host , port )
634
- HTTPConnection . connect (self )
633
+ self ._conn . _set_hostport (host , port )
634
+ self . _conn . connect ()
635
635
636
636
def set_debuglevel (self , debuglevel ):
637
637
"The class no longer supports the debuglevel."
@@ -643,8 +643,8 @@ def getfile(self):
643
643
644
644
def putheader (self , header , * values ):
645
645
"The superclass allows only one value argument."
646
- HTTPConnection . putheader (self , header ,
647
- string .joinfields (values , '\r \n \t ' ))
646
+ self . _conn . putheader (header ,
647
+ string .joinfields (values , '\r \n \t ' ))
648
648
649
649
def getreply (self ):
650
650
"""Compat definition since superclass does not define it.
@@ -655,14 +655,14 @@ def getreply(self):
655
655
- any RFC822 headers in the response from the server
656
656
"""
657
657
try :
658
- response = self .getresponse ()
658
+ response = self ._conn . getresponse ()
659
659
except BadStatusLine , e :
660
660
### hmm. if getresponse() ever closes the socket on a bad request,
661
661
### then we are going to have problems with self.sock
662
662
663
663
### should we keep this behavior? do people use it?
664
664
# keep the socket open (as a file), and return it
665
- self .file = self .sock .makefile ('rb' , 0 )
665
+ self .file = self ._conn . sock .makefile ('rb' , 0 )
666
666
667
667
# close our socket -- we want to restart after any protocol error
668
668
self .close ()
@@ -675,7 +675,7 @@ def getreply(self):
675
675
return response .status , response .reason , response .msg
676
676
677
677
def close (self ):
678
- HTTPConnection . close (self )
678
+ self . _conn . close ()
679
679
680
680
# note that self.file == response.fp, which gets closed by the
681
681
# superclass. just clear the object ref here.
@@ -684,6 +684,17 @@ def close(self):
684
684
### do it
685
685
self .file = None
686
686
687
+ if hasattr (socket , 'ssl' ):
688
+ class HTTPS (HTTP ):
689
+ """Compatibility with 1.5 httplib interface
690
+
691
+ Python 1.5.2 did not have an HTTPS class, but it defined an
692
+ interface for sending http requests that is also useful for
693
+ https.
694
+ """
695
+
696
+ _connection_class = HTTPSConnection
697
+
687
698
688
699
class HTTPException (Exception ):
689
700
pass
@@ -764,7 +775,7 @@ def test():
764
775
print h .getfile ().read ()
765
776
766
777
if hasattr (socket , 'ssl' ):
767
- host = 'www.c2 .net'
778
+ host = 'sourceforge .net'
768
779
hs = HTTPS ()
769
780
hs .connect (host )
770
781
hs .putrequest ('GET' , selector )
0 commit comments