title: Python FTPS hangs on directory list in passive mode
body: <p>I managed to connect to a FTP server using curl and list the content of the directory <code>out</code>:</p>
  
  <pre><code>$ curl -v --insecure --ftp-ssl --user xxx:yyy blabla:990/out/
  &gt; AUTH SSL
  &lt; 234 Proceed with negotiation.
  ...
  &gt; USER xxx
  &lt; 331 Please specify the password.
  &gt; PASS yyy
  &lt; 230 Login successful.
  &gt; PBSZ 0
  &lt; 200 PBSZ set to 0.
  &gt; PROT P
  &lt; 200 PROT now Private.
  &gt; PWD
  &lt; 257 "/"
  &gt; CWD out
  &lt; 250 Directory successfully changed.
  &gt; EPSV
  &lt; 229 Entering Extended Passive Mode (|||51042|).
  *   Trying aaa.bbb.ccc.ddd...
  * Connecting to aaa.bbb.ccc.ddd (aaa.bbb.ccc.ddd) port 51042
  * Connected to blabla (aaa.bbb.ccc.ddd) port 990 (#0)
  &gt; TYPE A
  &lt; 200 Switching to ASCII mode.
  &gt; LIST
  &lt; 150 Here comes the directory listing.
  * Maxdownload = -1
  * Doing the SSL/TLS handshake on the data stream
  * SSL re-using session ID
  * TLS 1.0 connection using TLS_RSA_WITH_AES_256_CBC_SHA
  * Server certificate: blabla
  * Server certificate: Symantec Class 3 Secure Server CA - G4
  * Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
  { [539 bytes data]
  100   539    0   539    0     0    900      0 --:--:-- --:--:-- --:--:--   899* Remembering we are in dir "out/"
  &lt; 226 Directory send OK.
  </code></pre>
  
  <p>I tried to do the same using python (2.7.10 on Mac OS X 10.10.5)</p>
  
  <pre><code>import ftplib
  
  ftps = ftplib.FTP_TLS()
  ftps.set_debuglevel(1)
  print ftps.set_pasv(True)
  print ftps.connect("blabla", 990)
  print ftps.login("xxx", "yyy")
  print ftps.sendcmd("PBSZ 0")
  print ftps.prot_p()
  print ftps.pwd()
  print ftps.cwd("out")
  print ftps.transfercmd("LIST")
  ftps.close()
  </code></pre>
  
  <p>But with python the <code>LIST command</code> hangs indefinitely</p>
  
  <pre><code>*cmd* 'AUTH TLS'
  *resp* '234 Proceed with negotiation.'
  *cmd* 'USER xxx'
  *resp* '331 Please specify the password.'
  *cmd* 'PASS ********\n'
  *resp* '230 Login successful.'
  230 Login successful.
  *cmd* 'PBSZ 0'
  *resp* '200 PBSZ set to 0.'
  200 PBSZ set to 0.
  *cmd* 'PBSZ 0'
  *resp* '200 PBSZ set to 0.'
  *cmd* 'PROT P'
  *resp* '200 PROT now Private.'
  200 PROT now Private.
  *cmd* 'PWD'
  *resp* '257 "/"'
  /
  *cmd* 'CWD out'
  *resp* '250 Directory successfully changed.'
  250 Directory successfully changed.
  *cmd* 'PASV'
  *resp* '227 Entering Passive Mode (aa,bb,cc,dd,199,97).'
  </code></pre>
  
  <p>Why could this be happening? </p>
  
  <p>UPDATE: Ok, I added the option <code>--disable-epsv</code> to my curl command and this fails, too. So the reason why python fails to list the directory is that is uses <code>PASV</code>. How can I force Python to use <code>EPSV</code> instead? </p>
  
  <p>UPDATE2: This bug <a href="https://github.com/python/cpython/pull/28" rel="nofollow">https://github.com/python/cpython/pull/28</a> seems to cause the hanging of Python ftplib when the IP address returned by PASV is wrong. This seems to be the problem in my case, too. But does anybody know a workaround how to force Python to use <code>EPSV</code> instead?</p>