try... except SyntaxError: unexpected EOF while parsing

Jerry Hill malaclypse2 at gmail.com
Wed Apr 4 13:59:32 EDT 2007


On 4 Apr 2007 10:38:24 -0700, oscartheduck <oscartheduck at gmail.com> 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"
try:
  sshport = int(sshport)
except ValueError:
  print "Not a valid port"
  sys.exit()

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


-- 
Jerry



More information about the Python-list mailing list