Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70297d3

Browse files
committed
Change the 227 response parser to use a more liberal regular
expression. This is needed for certain servers that (in violation of the standard) don't return the parentheses in the response. This fixes SF bug #441712 by Henrik Weber (not exactly using his patch).
1 parent 2dc0794 commit 70297d3

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

Lib/ftplib.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -551,21 +551,23 @@ def parse150(resp):
551551
return None
552552

553553

554+
_227_re = None
555+
554556
def parse227(resp):
555557
'''Parse the '227' response for a PASV request.
556558
Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)'
557559
Return ('host.addr.as.numbers', port#) tuple.'''
558560

559561
if resp[:3] != '227':
560562
raise error_reply, resp
561-
left = resp.find('(')
562-
if left < 0: raise error_proto, resp
563-
right = resp.find(')', left + 1)
564-
if right < 0:
565-
raise error_proto, resp # should contain '(h1,h2,h3,h4,p1,p2)'
566-
numbers = resp[left+1:right].split(',')
567-
if len(numbers) != 6:
563+
global _227_re
564+
if _227_re is None:
565+
import re
566+
_227_re = re.compile(r'(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)')
567+
m = _227_re.search(resp)
568+
if not m:
568569
raise error_proto, resp
570+
numbers = m.groups()
569571
host = '.'.join(numbers[:4])
570572
port = (int(numbers[4]) << 8) + int(numbers[5])
571573
return host, port

0 commit comments

Comments
 (0)