having trouble suppressing traceback on socket.error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnewlon
    New Member
    • Nov 2013
    • 5

    having trouble suppressing traceback on socket.error

    My python program is calling smtplib.sendmai l which ultimately calls socket.py to open port 25 on the specified host. My issue is that if an error is raised on that attempted socket connection, I can't seem to catch the exception and handle it gracefully - I get the traceback. 'except socket.error:' is being bypassed for some reason that I can't figure out. Thoughts on what I am doing wrong? Thanks!



    Code:
    s = smtplib.SMTP('10.255.208.122')
    
    try:
       s.sendmail(sender, receiver, msg.as_string())
       wrtevt('Success')
       s.quit()
    except socket.error:
       errno, errstr = sys.exc_info()[:2]
       if errno == socket.timeout:
         print "There was a timeout"
       else:
         print "There was some other socket error"


    Traceback (most recent call last):
    File "scaladir.p y", line 57, in <module>
    s = smtplib.SMTP('1 0.255.208.122')
    File "C:\Program Files (x86)\Python26\ lib\smtplib.py" , line 239, in __init__
    (code, msg) = self.connect(ho st, port)
    File "C:\Program Files (x86)\Python26\ lib\smtplib.py" , line 295, in connect
    self.sock = self._get_socke t(host, port, self.timeout)
    File "C:\Program Files (x86)\Python26\ lib\smtplib.py" , line 273, in _get_socke
    t
    return socket.create_c onnection((port , host), timeout)
    File "C:\Program Files (x86)\Python26\ lib\socket.py", line 561, in create_conn
    ection
    raise error, msg
    socket.error: [Errno 10013] An attempt was made to access a socket in a way forb
    idden by its access permissions
    Last edited by Rabbit; Nov 12 '13, 11:43 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • pnewlon
    New Member
    • Nov 2013
    • 5

    #2
    I ran a sniffer trace and I am not getting the typical 'TCP RST' from a refused connection. I am getting an ICMP type 3 code 10 - destination unreachable, host administrativel y prohibited.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Please use code tags when posting code or formatted data.

      The reason you are unable to trap the error is because the error is on line 1, outside of your try block.

      Comment

      • pnewlon
        New Member
        • Nov 2013
        • 5

        #4
        Sorry about that, I will do so!

        The address in line 1 does not have an SMTP server running, I am testing my code for error trapping when the server won't respond. If I change the IP address to one that is running an SMTP server, then the code runs fine.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Exactly. That's why it's erroring out and not being trapped.

          Comment

          • pnewlon
            New Member
            • Nov 2013
            • 5

            #6
            So there is no graceful way to prevent the program from blowing up when an attempt is made using smtplib to connect on port 25 to a device that has no listener on that port?

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              There is. Put your code that's causing the error in the try block.
              Last edited by Rabbit; Nov 13 '13, 04:51 PM.

              Comment

              • pnewlon
                New Member
                • Nov 2013
                • 5

                #8
                Perfect, that worked! Thank you for your assistance!

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  No problem, good luck with the rest of your project.

                  Comment

                  Working...