发生套接字错误时,如何从异常中获取其错误代码?

时间:2012-09-19 04:58:13

标签: python sockets exception error-handling error-code

这样的代码:

import socket, sys

try:
    address = ('127.0.0.1', 31500)  
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
    s.connect(address)  
except Exception:
    errType, errValue, errTraceback = sys.exc_info()
    # or
    # handle the Exception it throw out ?

我想得到的是像10060这样的错误代码,这意味着connection time out,感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

使用

except EnvironmentError as e:
    print e.errno