MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / ftp_open

Method ftp_open

tools/python-3.11.9-amd64/Lib/urllib/request.py:1532–1582  ·  view source on GitHub ↗
(self, req)

Source from the content-addressed store, hash-verified

1530
1531class FTPHandler(BaseHandler):
1532 def ftp_open(self, req):
1533 import ftplib
1534 import mimetypes
1535 host = req.host
1536 if not host:
1537 raise URLError('ftp error: no host given')
1538 host, port = _splitport(host)
1539 if port is None:
1540 port = ftplib.FTP_PORT
1541 else:
1542 port = int(port)
1543
1544 # username/password handling
1545 user, host = _splituser(host)
1546 if user:
1547 user, passwd = _splitpasswd(user)
1548 else:
1549 passwd = None
1550 host = unquote(host)
1551 user = user or ''
1552 passwd = passwd or ''
1553
1554 try:
1555 host = socket.gethostbyname(host)
1556 except OSError as msg:
1557 raise URLError(msg)
1558 path, attrs = _splitattr(req.selector)
1559 dirs = path.split('/')
1560 dirs = list(map(unquote, dirs))
1561 dirs, file = dirs[:-1], dirs[-1]
1562 if dirs and not dirs[0]:
1563 dirs = dirs[1:]
1564 try:
1565 fw = self.connect_ftp(user, passwd, host, port, dirs, req.timeout)
1566 type = file and 'I' or 'D'
1567 for attr in attrs:
1568 attr, value = _splitvalue(attr)
1569 if attr.lower() == 'type' and \
1570 value in ('a', 'A', 'i', 'I', 'd', 'D'):
1571 type = value.upper()
1572 fp, retrlen = fw.retrfile(file, type)
1573 headers = ""
1574 mtype = mimetypes.guess_type(req.full_url)[0]
1575 if mtype:
1576 headers += "Content-type: %s\n" % mtype
1577 if retrlen is not None and retrlen >= 0:
1578 headers += "Content-length: %d\n" % retrlen
1579 headers = email.message_from_string(headers)
1580 return addinfourl(fp, headers, req.full_url)
1581 except ftplib.all_errors as exp:
1582 raise URLError(exp) from exp
1583
1584 def connect_ftp(self, user, passwd, host, port, dirs, timeout):
1585 return ftpwrapper(user, passwd, host, port, dirs, timeout,

Callers

nothing calls this directly

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
listFunction · 0.85
upperMethod · 0.80
retrfileMethod · 0.80

Tested by

no test coverage detected