socket-python syntax error

0
  except socket.error, (errno, msg):
            if errno == 1:
                # Operation not permitted
                msg = msg + (
                    " - Note that ICMP messages can only be sent from processes"
                    " running as root."
                )
                raise socket.error(msg)
  

Traceback (most recent call last):

     

File "", line 1, in File   "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py",   line 699, in runfile       execfile (filename, namespace) File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py",   line 88, in execfile       exec (compile (open (filename, 'rb'). read (), filename, 'exec'), namespace) File "/home/epamos/Downloads/Gping2(1).py", line 78       except socket.error, (errno, msg):                          ^ SyntaxError: invalid syntax

I'm using python 3.5, removing the comma triggers 7 errors involving GPing.

    
asked by anonymous 05.04.2017 / 21:36

1 answer

2

This Except syntax is not valid in Python 3 - this is the old Python 2 syntax, and even in new code made to run in Python 2 it is still recommended to use the new style:

try:
   ...
except NomeDoErro as variavel:
   ...

In this case:

except socket.error as error:
    # codigo usando a variável error

(Note, I suggest you arrange the indentation of your Python code to use 4 spaces per block, always - rather than a varied indentation - you'll realize that it's a lot easier to read)     

17.04.2017 / 01:29