netrc --- pemrosesan berkas netrc

Kode sumber: Lib/netrc.py


Kelas netrc mengurai dan mengenkapsulasi format berkas netrc yang digunakan oleh program ftp Unix dan klien FTP lainnya.

class netrc.netrc([file])

A netrc instance or subclass instance encapsulates data from a netrc file. The initialization argument, if present, specifies the file to parse. If no argument is given, the file .netrc in the user's home directory -- as determined by os.path.expanduser() -- will be read. Otherwise, a FileNotFoundError exception will be raised. Parse errors will raise NetrcParseError with diagnostic information including the file name, line number, and terminating token. If no argument is specified on a POSIX system, the presence of passwords in the .netrc file will raise a NetrcParseError if the file ownership or permissions are insecure (owned by a user other than the user running the process, or accessible for read or write by any other user). This implements security behavior equivalent to that of ftp and other programs that use .netrc.

Berubah pada versi 3.4: Menambahkan pemeriksaan izin POSIX.

Berubah pada versi 3.7: os.path.expanduser() digunakan untuk menemukan lokasi berkas .netrc ketika file tidak diberikan sebagai argumen.

Berubah pada versi 3.10: netrc try UTF-8 encoding before using locale specific encoding. The entry in the netrc file no longer needs to contain all tokens. The missing tokens' value default to an empty string. All the tokens and their values now can contain arbitrary characters, like whitespace and non-ASCII characters. If the login name is anonymous, it won't trigger the security check.

exception netrc.NetrcParseError

Eksepsi yang dimunculkan oleh kelas netrc ketika galat sintaksis ditemukan dalam teks sumber. Instance eksepsi ini menyediakan tiga atribut yang menarik:

msg

Penjelasan tekstual tentang galat.

filename

Nama berkas sumber.

lineno

Nomor baris tempat galat ditemukan.

Objek netrc

Sebuah instance netrc memiliki metode-metode berikut:

netrc.authenticators(host)

Kembalikan 3-tuple (login, account, password) berisi autentikator untuk host. Jika berkas netrc tidak berisi entri untuk host yang diberikan, kembalikan tuple yang terkait dengan entri 'default'. Jika tidak ada host yang cocok maupun entri default yang tersedia, kembalikan None.

netrc.__repr__()

Tulis data kelas sebagai string dalam format berkas netrc. (Hal ini membuang komentar dan dapat menyusun ulang entri.)

Instance netrc memiliki variabel instance publik:

netrc.hosts

Dictionary yang memetakan nama host ke tuple (login, account, password). Entri 'default', jika ada, direpresentasikan sebagai pseudo-host dengan nama tersebut.

netrc.macros

Dictionary yang memetakan nama makro ke daftar string.