2

I have the following:

import netrc

machine = 'ftp.example.com'
auth = netrc.netrc().authenticators(machine)

I have the following line in my .netrc file:

machine "### comment"

I get the following error:

netrc.NetrcParseError: bad follower token 'comment"'

I can remove the comment, but I need to use this on other accounts where I can not edit the .netrc file, so what to do here?

kenorb
  • 155,785
  • 88
  • 678
  • 743
runrig
  • 6,486
  • 2
  • 27
  • 44
  • 1
    Unfortunately, the answer was to rewrite the netrc library, as the existing one uses a parser that makes it impossible to fix this issue. Also wrote a wrapper for the ftp library that transparently uses the netrc library. I'd like to make these freely available, but I'm not really sure where to put them. – runrig Aug 04 '15 at 22:10
  • 2
    why not github? – LangeHaare Aug 29 '17 at 17:28

2 Answers2

1

Doesn't your line need to be

machine ftp.example.com login user password pw

If you start the line with 'machine' you at least need a machine name, before you comment out the rest of the line, otherwise netrc can't parse the line.

robert_x44
  • 9,224
  • 1
  • 32
  • 37
  • No, using a bogus machine name is an accepted technique for getting comments into the file since lines that start with "#" mess up some ftp clients. Command line ftp handles these kind of comments just fine. – runrig Feb 26 '15 at 23:45
  • @runrig my point is that it looks like netrc is telling you that it needs a bogus machine name... not just 'machine' before your comment, it looks like it needs 'machine bogus'. This may just be the netrc library's limitation. – robert_x44 Feb 26 '15 at 23:57
  • The machine name is '### comment'. It shouldn't be the library's job to tell me what a valid machine name is. It should behave at the very least just like the ftp command line client. Maybe a bug report/patch is in order. – runrig Feb 27 '15 at 00:13
  • @runrig I don't disagree with you, but that seems to be what netrc chokes on, have you tried putting a name in front of the comment? - does that fix the parse error? – robert_x44 Feb 27 '15 at 00:21
  • Actually it's even worse. It chokes on an entry of 'machine bogus' without a 'password'. The netrc man pages do not say that login or password are required. You also can not quote fields, e.g., use a password with spaces in it. – runrig Feb 27 '15 at 17:18
0

I believe it's parsing the line without treating the quotation marks any differently from other non-whitespace characters. So this:

machine "### comment"

gets turned into the tokens machine, "###, and comment". After that, it can't find any more tokens, and it barfs.

Unfortunately, since there are lots of netrc parsers out there and they might all have different handling of edge cases, you can't really even solve this by writing your own parser unless you can get that parser used by your favorite tools (curl, pip, etc.).

As a further aside on comments, apparently the Python netrc module will recognize # machine ... (with a space) but not #machine ... (without a space) as a comment line. The latter results in various errors like netrc.NetrcParseError: bad toplevel token '...', which tools like pip seem to hide from you, pretending there are no entries in the file.

Ken Williams
  • 22,756
  • 10
  • 85
  • 147