try... except SyntaxError: unexpected EOF while parsing

Terry Reedy tjreedy at udel.edu
Wed Apr 4 13:54:40 EDT 2007


"oscartheduck" <oscartheduck at gmail.com> wrote in message 
news:1175708304.034078.139020 at e65g2000hsc.googlegroups.com...
|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.
|
| Here's my code:
|
| #!/usr/bin/env python
| import sys
| import os
|
| 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:
|  if port > 65535:
|    print "That's not a valid port number, sorry. Between 0 and 65535
| is cool."
|    sys.exit()
|  else:
|    cmd = 'su root -c "/usr/sbin/sshd -p %s"' % port
| except SyntaxError:
|      cmd = 'su root -c "/usr/sbin/sshd -p 2024;exit"'
|
| os.system(cmd)
|
|
| 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?

Try putting the input statement after the try statement.






More information about the Python-list mailing list