21
21
from test .support import HOST
22
22
threading = support .import_module ('threading' )
23
23
24
+ TIMEOUT = 3
24
25
# the dummy data returned by server over the data channel when
25
26
# RETR, LIST, NLST, MLSD commands are issued
26
27
RETR_DATA = 'abcde12345\r \n ' * 1000
@@ -125,15 +126,15 @@ def cmd_port(self, arg):
125
126
addr = list (map (int , arg .split (',' )))
126
127
ip = '%d.%d.%d.%d' % tuple (addr [:4 ])
127
128
port = (addr [4 ] * 256 ) + addr [5 ]
128
- s = socket .create_connection ((ip , port ), timeout = 2 )
129
+ s = socket .create_connection ((ip , port ), timeout = TIMEOUT )
129
130
self .dtp = self .dtp_handler (s , baseclass = self )
130
131
self .push ('200 active data connection established' )
131
132
132
133
def cmd_pasv (self , arg ):
133
134
with socket .socket () as sock :
134
135
sock .bind ((self .socket .getsockname ()[0 ], 0 ))
135
136
sock .listen (5 )
136
- sock .settimeout (10 )
137
+ sock .settimeout (TIMEOUT )
137
138
ip , port = sock .getsockname ()[:2 ]
138
139
ip = ip .replace ('.' , ',' ); p1 = port / 256 ; p2 = port % 256
139
140
self .push ('227 entering passive mode (%s,%d,%d)' % (ip , p1 , p2 ))
@@ -143,15 +144,15 @@ def cmd_pasv(self, arg):
143
144
def cmd_eprt (self , arg ):
144
145
af , ip , port = arg .split (arg [0 ])[1 :- 1 ]
145
146
port = int (port )
146
- s = socket .create_connection ((ip , port ), timeout = 2 )
147
+ s = socket .create_connection ((ip , port ), timeout = TIMEOUT )
147
148
self .dtp = self .dtp_handler (s , baseclass = self )
148
149
self .push ('200 active data connection established' )
149
150
150
151
def cmd_epsv (self , arg ):
151
152
with socket .socket (socket .AF_INET6 ) as sock :
152
153
sock .bind ((self .socket .getsockname ()[0 ], 0 ))
153
154
sock .listen (5 )
154
- sock .settimeout (10 )
155
+ sock .settimeout (TIMEOUT )
155
156
port = sock .getsockname ()[1 ]
156
157
self .push ('229 entering extended passive mode (|||%d|)' % port )
157
158
conn , addr = sock .accept ()
@@ -454,7 +455,7 @@ class TestFTPClass(TestCase):
454
455
def setUp (self ):
455
456
self .server = DummyFTPServer ((HOST , 0 ))
456
457
self .server .start ()
457
- self .client = ftplib .FTP (timeout = 2 )
458
+ self .client = ftplib .FTP (timeout = TIMEOUT )
458
459
self .client .connect (self .server .host , self .server .port )
459
460
460
461
def tearDown (self ):
@@ -667,7 +668,7 @@ def test_makeport(self):
667
668
668
669
def test_makepasv (self ):
669
670
host , port = self .client .makepasv ()
670
- conn = socket .create_connection ((host , port ), 10 )
671
+ conn = socket .create_connection ((host , port ), timeout = TIMEOUT )
671
672
conn .close ()
672
673
# IPv4 is in use, just make sure send_epsv has not been used
673
674
self .assertEqual (self .server .handler_instance .last_received_cmd , 'pasv' )
@@ -685,15 +686,15 @@ def is_client_connected():
685
686
return True
686
687
687
688
# base test
688
- with ftplib .FTP (timeout = 2 ) as self .client :
689
+ with ftplib .FTP (timeout = TIMEOUT ) as self .client :
689
690
self .client .connect (self .server .host , self .server .port )
690
691
self .client .sendcmd ('noop' )
691
692
self .assertTrue (is_client_connected ())
692
693
self .assertEqual (self .server .handler_instance .last_received_cmd , 'quit' )
693
694
self .assertFalse (is_client_connected ())
694
695
695
696
# QUIT sent inside the with block
696
- with ftplib .FTP (timeout = 2 ) as self .client :
697
+ with ftplib .FTP (timeout = TIMEOUT ) as self .client :
697
698
self .client .connect (self .server .host , self .server .port )
698
699
self .client .sendcmd ('noop' )
699
700
self .client .quit ()
@@ -703,7 +704,7 @@ def is_client_connected():
703
704
# force a wrong response code to be sent on QUIT: error_perm
704
705
# is expected and the connection is supposed to be closed
705
706
try :
706
- with ftplib .FTP (timeout = 2 ) as self .client :
707
+ with ftplib .FTP (timeout = TIMEOUT ) as self .client :
707
708
self .client .connect (self .server .host , self .server .port )
708
709
self .client .sendcmd ('noop' )
709
710
self .server .handler_instance .next_response = '550 error on quit'
@@ -759,7 +760,7 @@ class TestIPv6Environment(TestCase):
759
760
def setUp (self ):
760
761
self .server = DummyFTPServer (('::1' , 0 ), af = socket .AF_INET6 )
761
762
self .server .start ()
762
- self .client = ftplib .FTP ()
763
+ self .client = ftplib .FTP (timeout = TIMEOUT )
763
764
self .client .connect (self .server .host , self .server .port )
764
765
765
766
def tearDown (self ):
@@ -776,7 +777,7 @@ def test_makeport(self):
776
777
777
778
def test_makepasv (self ):
778
779
host , port = self .client .makepasv ()
779
- conn = socket .create_connection ((host , port ), 10 )
780
+ conn = socket .create_connection ((host , port ), timeout = TIMEOUT )
780
781
conn .close ()
781
782
self .assertEqual (self .server .handler_instance .last_received_cmd , 'epsv' )
782
783
@@ -802,7 +803,7 @@ class TestTLS_FTPClassMixin(TestFTPClass):
802
803
def setUp (self ):
803
804
self .server = DummyTLS_FTPServer ((HOST , 0 ))
804
805
self .server .start ()
805
- self .client = ftplib .FTP_TLS (timeout = 2 )
806
+ self .client = ftplib .FTP_TLS (timeout = TIMEOUT )
806
807
self .client .connect (self .server .host , self .server .port )
807
808
# enable TLS
808
809
self .client .auth ()
@@ -815,7 +816,7 @@ class TestTLS_FTPClass(TestCase):
815
816
def setUp (self ):
816
817
self .server = DummyTLS_FTPServer ((HOST , 0 ))
817
818
self .server .start ()
818
- self .client = ftplib .FTP_TLS (timeout = 2 )
819
+ self .client = ftplib .FTP_TLS (timeout = TIMEOUT )
819
820
self .client .connect (self .server .host , self .server .port )
820
821
821
822
def tearDown (self ):
@@ -875,7 +876,7 @@ def test_context(self):
875
876
self .assertRaises (ValueError , ftplib .FTP_TLS , certfile = CERTFILE ,
876
877
keyfile = CERTFILE , context = ctx )
877
878
878
- self .client = ftplib .FTP_TLS (context = ctx , timeout = 2 )
879
+ self .client = ftplib .FTP_TLS (context = ctx , timeout = TIMEOUT )
879
880
self .client .connect (self .server .host , self .server .port )
880
881
self .assertNotIsInstance (self .client .sock , ssl .SSLSocket )
881
882
self .client .auth ()
0 commit comments