try... except SyntaxError: unexpected EOF while parsing

O

oscartheduck

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?
 
K

kyosohma

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 sticking your

port = input("Please enter a port to connect on. If you're unsure or
just want the default of port 2024 just hit enter -- ")

inside your "try" block instead. Or switch to using the "raw_input"
builtin instead of "input".

Mike
 
T

Terry Reedy

|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.
 
S

Steve Holden

oscartheduck said:
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?
Yes. SyntaxError is raised when the interpreter is compiling the Python
into byetcodes ready for execution. This *can* happen at run time, but
usually it's when you are importing a module and so it gets
transmogrified into an ImportError, though you can trigger it with, for
example, eval:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
print < m s q
^
SyntaxError: invalid syntax
However, if you run your program and give it an invalid port number
surely the resulting traceback would *tell* you what exception you need
to trap if you used a %d substituion rather than a %s. Since pretty much
anything is valid for a %s substitution the problem is that you will be
calling os.system() on a command with a non-numeric port number and not
catching the resulting exception.

Using input(), by the way, is a recipe for disaster with non-trustworthy
users. I'd much prefer to use raw_input() and convert explicitly to integer.

The easiest way to proceed would then be to perform sensible error
checking on the port number before proceeding:

port = raw_input("Port # (enter for 2204 default: ")
if port == "":
port = 2204
try:
port = int(port)
except ValueError:
raise ValueError("Port number must be numeric")

or something like that. Really you should be in a loop which terminated
when you get an acceptable port number.

regards
Steve
 
S

skip

(Are you Howard the Duck's lesser known cousin?)

oscar> I have a small script for doing some ssh stuff for me. I could
oscar> have written it as shell script, but wanted to improve my python
oscar> skills some.

oscar> RIght now, I'm not catching a syntax error as I'd like to.

Well, you should probably never catch SyntaxError (unless you use exec or
eval, both of which you should also probably never use), however... What
you're describing is not a Python syntax error. In addition, if you're
expecting to catch problems with the call to input("Please ...") you'll need
to have it within the try block.

Given that port numbers have to be ints I'd try something like:

port = 2024
userport = raw_input("Please enter a port #: ").strip()
if userport:
try:
port = int(userport)
except ValueError:
pass
if port > 65535:
blah blah blah
...

Note that input("prompt") is equivalent to eval(raw_input("prompt")), so you
should probably never use it. (It's going away in Python 3.0 anyway.)

Skip
 
J

Jerry Hill

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
 
O

oscartheduck

Wow. Thanks, everyone, for the responses. It helps a lot having such a
well informed and helpful resource.
 
S

Steve Holden

Steve said:
oscartheduck wrote: [...]Yes. SyntaxError is raised when the interpreter is compiling the Python
into byetcodes ready for execution. This *can* happen at run time, but
usually it's when you are importing a module and so it gets
transmogrified into an ImportError, though you can trigger it with, for
example, eval:
Of course I omitted to notice that input() can also raise SyntaxError
.... sorry about that.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top