try... except SyntaxError: unexpected EOF while parsing

Jerry Hill malaclypse2 at
Wed Apr 4 19:59:32 CEST 2007

On 4 Apr 2007 10:38:24 -0700, oscartheduck <oscartheduck at> wrote:
> I have a small script for doing some ssh stuff for me. I could have
> written it as shell script, but wanted to improve my python skills
> some.
> RIght now, I'm not catching a syntax error as I'd like to.
> port = input("Please enter a port to connect on. If you're unsure or
> just want the default of port 2024 just hit enter  --  ")
> try:
> I'm under the impression that the except should catch the syntax error
> and execute the "default" command, but hitting enter at the input
> value doesn't lead to that. Any ideas what's going wrong?

You didn't include the full exception that you're getting, but my
guess is that the line trowing the error is the one calling input().
Move your try up before that line, and it should work as you expected.

On a slightly different note, input() isn't really the right function
to call here.  input() evaluates python commands.  Instead, use
raw_input() which just returns a user-entered string.

Something like this (untested):
sshport = raw_input('Port: ')
if sshport == "":
  sshport = "2024"
  sshport = int(sshport)
except ValueError:
  print "Not a valid port"

cmd = 'su root -c "/usr/sbin/sshd -p %s"' % port


More information about the Python-list mailing list