1from _typeshed import SupportsRead, SupportsReadline
2from socket import socket
3from ssl import SSLContext
4from typing import Any, BinaryIO, Callable, List, Optional, Text, Tuple, Type, TypeVar, Union
5from typing_extensions import Literal
6
7_T = TypeVar("_T")
8_IntOrStr = Union[int, Text]
9
10MSG_OOB: int
11FTP_PORT: int
12MAXLINE: int
13CRLF: str
14
15class Error(Exception): ...
16class error_reply(Error): ...
17class error_temp(Error): ...
18class error_perm(Error): ...
19class error_proto(Error): ...
20
21all_errors: Tuple[Type[Exception], ...]
22
23class FTP:
24    debugging: int
25
26    # Note: This is technically the type that's passed in as the host argument.  But to make it easier in Python 2 we
27    # accept Text but return str.
28    host: str
29
30    port: int
31    maxline: int
32    sock: Optional[socket]
33    welcome: Optional[str]
34    passiveserver: int
35    timeout: int
36    af: int
37    lastresp: str
38
39    file: Optional[BinaryIO]
40    def __init__(
41        self, host: Text = ..., user: Text = ..., passwd: Text = ..., acct: Text = ..., timeout: float = ...
42    ) -> None: ...
43    def connect(self, host: Text = ..., port: int = ..., timeout: float = ...) -> str: ...
44    def getwelcome(self) -> str: ...
45    def set_debuglevel(self, level: int) -> None: ...
46    def debug(self, level: int) -> None: ...
47    def set_pasv(self, val: Union[bool, int]) -> None: ...
48    def sanitize(self, s: Text) -> str: ...
49    def putline(self, line: Text) -> None: ...
50    def putcmd(self, line: Text) -> None: ...
51    def getline(self) -> str: ...
52    def getmultiline(self) -> str: ...
53    def getresp(self) -> str: ...
54    def voidresp(self) -> str: ...
55    def abort(self) -> str: ...
56    def sendcmd(self, cmd: Text) -> str: ...
57    def voidcmd(self, cmd: Text) -> str: ...
58    def sendport(self, host: Text, port: int) -> str: ...
59    def sendeprt(self, host: Text, port: int) -> str: ...
60    def makeport(self) -> socket: ...
61    def makepasv(self) -> Tuple[str, int]: ...
62    def login(self, user: Text = ..., passwd: Text = ..., acct: Text = ...) -> str: ...
63    # In practice, `rest` rest can actually be anything whose str() is an integer sequence, so to make it simple we allow integers.
64    def ntransfercmd(self, cmd: Text, rest: Optional[_IntOrStr] = ...) -> Tuple[socket, int]: ...
65    def transfercmd(self, cmd: Text, rest: Optional[_IntOrStr] = ...) -> socket: ...
66    def retrbinary(
67        self, cmd: Text, callback: Callable[[bytes], Any], blocksize: int = ..., rest: Optional[_IntOrStr] = ...
68    ) -> str: ...
69    def storbinary(
70        self,
71        cmd: Text,
72        fp: SupportsRead[bytes],
73        blocksize: int = ...,
74        callback: Optional[Callable[[bytes], Any]] = ...,
75        rest: Optional[_IntOrStr] = ...,
76    ) -> str: ...
77    def retrlines(self, cmd: Text, callback: Optional[Callable[[str], Any]] = ...) -> str: ...
78    def storlines(self, cmd: Text, fp: SupportsReadline[bytes], callback: Optional[Callable[[bytes], Any]] = ...) -> str: ...
79    def acct(self, password: Text) -> str: ...
80    def nlst(self, *args: Text) -> List[str]: ...
81    # Technically only the last arg can be a Callable but ...
82    def dir(self, *args: Union[str, Callable[[str], None]]) -> None: ...
83    def rename(self, fromname: Text, toname: Text) -> str: ...
84    def delete(self, filename: Text) -> str: ...
85    def cwd(self, dirname: Text) -> str: ...
86    def size(self, filename: Text) -> Optional[int]: ...
87    def mkd(self, dirname: Text) -> str: ...
88    def rmd(self, dirname: Text) -> str: ...
89    def pwd(self) -> str: ...
90    def quit(self) -> str: ...
91    def close(self) -> None: ...
92
93class FTP_TLS(FTP):
94    def __init__(
95        self,
96        host: Text = ...,
97        user: Text = ...,
98        passwd: Text = ...,
99        acct: Text = ...,
100        keyfile: Optional[str] = ...,
101        certfile: Optional[str] = ...,
102        context: Optional[SSLContext] = ...,
103        timeout: float = ...,
104        source_address: Optional[Tuple[str, int]] = ...,
105    ) -> None: ...
106    ssl_version: int
107    keyfile: Optional[str]
108    certfile: Optional[str]
109    context: SSLContext
110    def login(self, user: Text = ..., passwd: Text = ..., acct: Text = ..., secure: bool = ...) -> str: ...
111    def auth(self) -> str: ...
112    def prot_p(self) -> str: ...
113    def prot_c(self) -> str: ...
114
115class Netrc:
116    def __init__(self, filename: Optional[Text] = ...) -> None: ...
117    def get_hosts(self) -> List[str]: ...
118    def get_account(self, host: Text) -> Tuple[Optional[str], Optional[str], Optional[str]]: ...
119    def get_macros(self) -> List[str]: ...
120    def get_macro(self, macro: Text) -> Tuple[str, ...]: ...
121
122def parse150(resp: str) -> Optional[int]: ...  # undocumented
123def parse227(resp: str) -> Tuple[str, int]: ...  # undocumented
124def parse229(resp: str, peer: Any) -> Tuple[str, int]: ...  # undocumented
125def parse257(resp: str) -> str: ...  # undocumented
126def ftpcp(
127    source: FTP, sourcename: str, target: FTP, targetname: str = ..., type: Literal["A", "I"] = ...
128) -> None: ...  # undocumented
129