(self, req)
| 1530 | |
| 1531 | class 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, |
nothing calls this directly
no test coverage detected