13.4. netrc — Arquivo de processamento netrc

Novo na versão 1.5.2.

Código-fonte: Lib/netrc.py


A classe netrc analisa e encapsula o formato do arquivo netrc usado pelo programa Unix ftp e outros clientes FTP.

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 will be read. 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.

Alterado na versão 2.7.6: Added the POSIX permissions check.

exception netrc.NetrcParseError

Exceção levantada pela classe netrc quando erros sintáticos são encontrados no texto de origem. As instâncias desta exceção fornecem três atributos interessantes: msg é uma explicação textual do erro, filename é o nome do arquivo-fonte e lineno fornece o número da linha na qual o erro foi encontrado.

13.4.1. Objetos netrc

Uma instância da classe netrc tem os seguintes métodos:

netrc.authenticators(host)

Retorna uma tupla de 3 elementos (login, conta, senha) dos autenticadores do host. Se o arquivo netrc não contém uma entrada para o host dado, retorna a tupla associada com a entrada padrão. Se não houver nenhum host correspondente nem uma entrada padrão estiver disponível, retorna None.

netrc.__repr__()

Despeja os dados da classe como uma string no formato de um arquivo netrc. (Isso descarta os comentários e pode reordenar as entradas.)

Instâncias de netrc possuem variáveis de instância públicas:

netrc.hosts

Dicionário mapeando nomes de host para tuplas (login, conta, senha). A entrada default, se houver, é representada como um pseudo-host por esse nome.

netrc.macros

Dicionário mapeando nomes de macros para listas de strings.

Nota

Passwords are limited to a subset of the ASCII character set. Versions of this module prior to 2.3 were extremely limited. Starting with 2.3, all ASCII punctuation is allowed in passwords. However, note that whitespace and non-printable characters are not allowed in passwords. This is a limitation of the way the .netrc file is parsed and may be removed in the future.