MCPcopy Index your code
hub / github.com/RustPython/RustPython / ftp_open

Method ftp_open

Lib/urllib/request.py:1506–1561  ·  view source on GitHub ↗
(self, req)

Source from the content-addressed store, hash-verified

1504
1505class FTPHandler(BaseHandler):
1506 def ftp_open(self, req):
1507 import ftplib
1508 import mimetypes
1509 host = req.host
1510 if not host:
1511 raise URLError('ftp error: no host given')
1512 host, port = _splitport(host)
1513 if port is None:
1514 port = ftplib.FTP_PORT
1515 else:
1516 port = int(port)
1517
1518 # username/password handling
1519 user, host = _splituser(host)
1520 if user:
1521 user, passwd = _splitpasswd(user)
1522 else:
1523 passwd = None
1524 host = unquote(host)
1525 user = user or ''
1526 passwd = passwd or ''
1527
1528 try:
1529 host = socket.gethostbyname(host)
1530 except OSError as msg:
1531 raise URLError(msg)
1532 path, attrs = _splitattr(req.selector)
1533 dirs = path.split('/')
1534 dirs = list(map(unquote, dirs))
1535 dirs, file = dirs[:-1], dirs[-1]
1536 if dirs and not dirs[0]:
1537 dirs = dirs[1:]
1538 fw = None
1539 try:
1540 fw = self.connect_ftp(user, passwd, host, port, dirs, req.timeout)
1541 type = file and 'I' or 'D'
1542 for attr in attrs:
1543 attr, value = _splitvalue(attr)
1544 if attr.lower() == 'type' and \
1545 value in ('a', 'A', 'i', 'I', 'd', 'D'):
1546 type = value.upper()
1547 fp, retrlen = fw.retrfile(file, type)
1548 headers = ""
1549 mtype = mimetypes.guess_type(req.full_url)[0]
1550 if mtype:
1551 headers += "Content-type: %s\n" % mtype
1552 if retrlen is not None and retrlen >= 0:
1553 headers += "Content-length: %d\n" % retrlen
1554 headers = email.message_from_string(headers)
1555 return addinfourl(fp, headers, req.full_url)
1556 except Exception as exp:
1557 if fw is not None and not fw.keepalive:
1558 fw.close()
1559 if isinstance(exp, ftplib.all_errors):
1560 raise URLError(f"ftp error: {exp}") from exp
1561 raise
1562
1563 def connect_ftp(self, user, passwd, host, port, dirs, timeout):

Callers 1

test_ftpMethod · 0.80

Calls 15

connect_ftpMethod · 0.95
URLErrorClass · 0.90
_splitportFunction · 0.90
_splituserFunction · 0.90
_splitpasswdFunction · 0.90
unquoteFunction · 0.90
_splitattrFunction · 0.90
_splitvalueFunction · 0.90
addinfourlClass · 0.90
listClass · 0.85
isinstanceFunction · 0.85
splitMethod · 0.45

Tested by 1

test_ftpMethod · 0.64