errno模块

我们都知道一些常用的异常类型,比如说(IOError,Exception,AttributeError),这些都是大部分标准的错误,细分下来又有不同的类型错误,这时候你需要能够区分一个更细分不同的错误,当然你也可以创建一个自定义的异常,但通常不必要的。回正题,那么我们该如何引发这些异常,如何获取完整的Error列表以及所描述的意思呢。

Errno 2: No such file or directory
Errno 122: Disk quota exceeded
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

如何获取python中异常类型的错误列表(Errno)

import errno
#获取错误名称以字典形式。
print({i:os.strerror(i) for i in sorted(errno.errorcode)})
#获取错误名称对应的错误描述。(在os x)
print(errno.errorcode)
#获取错误名称对应的错误描述。(在windows)
print(sorted(errno.errorcode.items(),key=operator.itemgetter(0)))

1.获取错误名称以字典形式。

{1: 'EPERM', 2: 'ENOENT', 3: 'ESRCH', 4: 'EINTR', 5: 'EIO', 6: 'ENXIO', 7: 'E2BIG', 8: 'ENOEXEC', 9: 'EBADF', 10: 'ECHILD', 11: 'EDEADLK', 12: 'ENOMEM', 13: 'EACCES', 14: 'EFAULT', 15: 'ENOTBLK', 16: 'EBUSY', 17: 'EEXIST', 18: 'EXDEV', 19: 'ENODEV', 20: 'ENOTDIR', 21: 'EISDIR', 22: 'EINVAL', 23: 'ENFILE', 24: 'EMFILE', 25: 'ENOTTY', 26: 'ETXTBSY', 27: 'EFBIG', 28: 'ENOSPC', 29: 'ESPIPE', 30: 'EROFS', 31: 'EMLINK', 32: 'EPIPE', 33: 'EDOM', 34: 'ERANGE', 35: 'EAGAIN', 36: 'EINPROGRESS', 37: 'EALREADY', 38: 'ENOTSOCK', 39: 'EDESTADDRREQ', 40: 'EMSGSIZE', 41: 'EPROTOTYPE', 42: 'ENOPROTOOPT', 43: 'EPROTONOSUPPORT', 44: 'ESOCKTNOSUPPORT', 46: 'EPFNOSUPPORT', 47: 'EAFNOSUPPORT', 48: 'EADDRINUSE', 49: 'EADDRNOTAVAIL', 50: 'ENETDOWN', 51: 'ENETUNREACH', 52: 'ENETRESET', 53: 'ECONNABORTED', 54: 'ECONNRESET', 55: 'ENOBUFS', 56: 'EISCONN', 57: 'ENOTCONN', 58: 'ESHUTDOWN', 59: 'ETOOMANYREFS', 60: 'ETIMEDOUT', 61: 'ECONNREFUSED', 62: 'ELOOP', 63: 'ENAMETOOLONG', 64: 'EHOSTDOWN', 65: 'EHOSTUNREACH', 66: 'ENOTEMPTY', 68: 'EUSERS', 69: 'EDQUOT', 70: 'ESTALE', 71: 'EREMOTE', 77: 'ENOLCK', 78: 'ENOSYS', 84: 'EOVERFLOW', 90: 'EIDRM', 91: 'ENOMSG', 92: 'EILSEQ', 94: 'EBADMSG', 95: 'EMULTIHOP', 96: 'ENODATA', 97: 'ENOLINK', 98: 'ENOSR', 99: 'ENOSTR', 100: 'EPROTO', 101: 'ETIME', 102: 'EOPNOTSUPP'}

2.获取错误名称对应的错误描述。
在OS X

{1: 'Operation not permitted', 2: 'No such file or directory', 3: 'No such process', 4: 'Interrupted system call', 5: 'Input/output error', 6: 'Device not configured', 7: 'Argument list too long', 8: 'Exec format error', 9: 'Bad file descriptor', 10: 'No child processes', 11: 'Resource deadlock avoided', 12: 'Cannot allocate memory', 13: 'Permission denied', 14: 'Bad address', 15: 'Block device required', 16: 'Resource busy', 17: 'File exists', 18: 'Cross-device link', 19: 'Operation not supported by device', 20: 'Not a directory', 21: 'Is a directory', 22: 'Invalid argument', 23: 'Too many open files in system', 24: 'Too many open files', 25: 'Inappropriate ioctl for device', 26: 'Text file busy', 27: 'File too large', 28: 'No space left on device', 29: 'Illegal seek', 30: 'Read-only file system', 31: 'Too many links', 32: 'Broken pipe', 33: 'Numerical argument out of domain', 34: 'Result too large', 35: 'Resource temporarily unavailable', 36: 'Operation now in progress', 37: 'Operation already in progress', 38: 'Socket operation on non-socket', 39: 'Destination address required', 40: 'Message too long', 41: 'Protocol wrong type for socket', 42: 'Protocol not available', 43: 'Protocol not supported', 44: 'Socket type not supported', 46: 'Protocol family not supported', 47: 'Address family not supported by protocol family', 48: 'Address already in use', 49: "Can't assign requested address", 50: 'Network is down', 51: 'Network is unreachable', 52: 'Network dropped connection on reset', 53: 'Software caused connection abort', 54: 'Connection reset by peer', 55: 'No buffer space available', 56: 'Socket is already connected', 57: 'Socket is not connected', 58: "Can't send after socket shutdown", 59: "Too many references: can't splice", 60: 'Operation timed out', 61: 'Connection refused', 62: 'Too many levels of symbolic links', 63: 'File name too long', 64: 'Host is down', 65: 'No route to host', 66: 'Directory not empty', 68: 'Too many users', 69: 'Disc quota exceeded', 70: 'Stale NFS file handle', 71: 'Too many levels of remote in path', 77: 'No locks available', 78: 'Function not implemented', 84: 'Value too large to be stored in data type', 90: 'Identifier removed', 91: 'No message of desired type', 92: 'Illegal byte sequence', 94: 'Bad message', 95: 'EMULTIHOP (Reserved)', 96: 'No message available on STREAM', 97: 'ENOLINK (Reserved)', 98: 'No STREAM resources', 99: 'Not a STREAM', 100: 'Protocol error', 101: 'STREAM ioctl timeout', 102: 'Operation not supported on socket'}**

3.获取错误名称对应的错误描述。(在windows)

[(1, 'EPERM'), (2, 'ENOENT'), (3, 'ESRCH'), (4, 'EINTR'), (5, 'EIO'), (6, 'ENXIO'), (7, 'E2BIG'), (8, 'ENOEXEC'), (9, 'EBADF'), (10, 'ECHILD'), (11, 'EAGAIN'), (12, 'ENOMEM'), (13, 'EACCES'), (14, 'EFAULT'), (16, 'EBUSY'), (17, 'EEXIST'), (18, 'EXDEV'), (19, 'ENODEV'), (20, 'ENOTDIR'), (21, 'EISDIR'), (22, 'EINVAL'), (23, 'ENFILE'), (24, 'EMFILE'), (25, 'ENOTTY'), (27, 'EFBIG'), (28, 'ENOSPC'), (29, 'ESPIPE'), (30, 'EROFS'), (31, 'EMLINK'), (32, 'EPIPE'), (33, 'EDOM'), (34, 'ERANGE'), (36, 'EDEADLOCK'), (38, 'ENAMETOOLONG'), (39, 'ENOLCK'), (40, 'ENOSYS'), (41, 'ENOTEMPTY'), (42, 'EILSEQ'), (104, 'EBADMSG'), (105, 'ECANCELED'), (111, 'EIDRM'), (120, 'ENODATA'), (121, 'ENOLINK'), (122, 'ENOMSG'), (124, 'ENOSR'), (125, 'ENOSTR'), (127, 'ENOTRECOVERABLE'), (129, 'ENOTSUP'), (132, 'EOVERFLOW'), (133, 'EOWNERDEAD'), (134, 'EPROTO'), (137, 'ETIME'), (139, 'ETXTBSY'), (10000, 'WSABASEERR'), (10004, 'WSAEINTR'), (10009, 'WSAEBADF'), (10013, 'WSAEACCES'), (10014, 'WSAEFAULT'), (10022, 'WSAEINVAL'), (10024, 'WSAEMFILE'), (10035, 'WSAEWOULDBLOCK'), (10036, 'WSAEINPROGRESS'), (10037, 'WSAEALREADY'), (10038, 'WSAENOTSOCK'), (10039, 'WSAEDESTADDRREQ'), (10040, 'WSAEMSGSIZE'), (10041, 'WSAEPROTOTYPE'), (10042, 'WSAENOPROTOOPT'), (10043, 'WSAEPROTONOSUPPORT'), (10044, 'WSAESOCKTNOSUPPORT'), (10045, 'WSAEOPNOTSUPP'), (10046, 'WSAEPFNOSUPPORT'), (10047, 'WSAEAFNOSUPPORT'), (10048, 'WSAEADDRINUSE'), (10049, 'WSAEADDRNOTAVAIL'), (10050, 'WSAENETDOWN'), (10051, 'WSAENETUNREACH'), (10052, 'WSAENETRESET'), (10053, 'WSAECONNABORTED'), (10054, 'WSAECONNRESET'), (10055, 'WSAENOBUFS'), (10056, 'WSAEISCONN'), (10057, 'WSAENOTCONN'), (10058, 'WSAESHUTDOWN'), (10059, 'WSAETOOMANYREFS'), (10060, 'WSAETIMEDOUT'), (10061, 'WSAECONNREFUSED'), (10062, 'WSAELOOP'), (10063, 'WSAENAMETOOLONG'), (10064, 'WSAEHOSTDOWN'), (10065, 'WSAEHOSTUNREACH'), (10066, 'WSAENOTEMPTY'), (10067, 'WSAEPROCLIM'), (10068, 'WSAEUSERS'), (10069, 'WSAEDQUOT'), (10070, 'WSAESTALE'), (10071, 'WSAEREMOTE'), (10091, 'WSASYSNOTREADY'), (10092, 'WSAVERNOTSUPPORTED'), (10093, 'WSANOTINITIALISED'), (10101, 'WSAEDISCON')]
#!/usr/bin/python3.6.4
#-*-coding:utf-8-*-

import socket
import sys
import os
import errno

__author__ = 'Rosefinch'
__date__ = '2018/5/31 16:27'

host_err = "www.google.com" #国内无法连接
host = "www.baidu.com" #国内连接上
# host = "localhost"
port = 80


dst_addr_err  = socket.gethostbyname(host_err) 
dst_addr  = socket.gethostbyname(host)

try:
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect((dst_addr,port))
except socket.error as e:
    if e.errno == 10061: #本地计算机没有打开80端口,去掉注释host = "localhost",将执行它。
        print("%s ----> %s ----> 由于目标计算机积极拒绝,无法连接" % (host,dst_addr))
else:
    print("%s ----> %s ----> Connection success!" % (host,dst_addr))

try:
    ss = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    ss.connect((dst_addr_err,port))
except socket.error as e:
    if e.errno == 10060:
        print("%s ----> %s ----> 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败" % (host_err,dst_addr_err))
else:
    print("%s ----> %s ----> Connection success!" % (host_err,dst_addr_err))

在windows机器测试:

#测试结果
www.baidu.com ----> 163.177.151.109 ----> Connection success!
www.google.com ----> 61.91.161.217 ----> 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败
[Finished in 21.2s]

注意:如果我们与特定的errno值进行比较(在其他操作系统/平台上可能不同)


符号

定义的符号的具体列表可用 errno.errorcode.keys(),可用的符号可以包括: 参考官网

errno.EPERM 无权操作(1)
  
errno.ENOENT 没有该文件或目录(2)
  
errno.ESRCH 无此进程(3)
  
errno.EINTR 系统调用被打断(4)
  
errno.EIO I/O 错误(5)
  
errno.ENXIO 无此设备或地址(6)
  
errno.E2BIG 参数列表过长(7)
  
errno.ENOEXEC Exec 格式错误
  
errno.EBADF 文件个数出错
  
errno.ECHILD 没有子进程(10)
  
errno.EAGAIN 重试(11)
  
errno.ENOMEM 内存不足(Out of memory)(
  
errno.EACCES 访问权限不足(13)  
  
errno.EFAULT 地址错误(14)
  
errno.ENOTBLK 需要块设备
  
errno.EBUSY 设备或资源正忙
  
errno.EEXIST 文件已存在
  
errno.EXDEV 跨设备符号(Cross-device link)
  
errno.ENODEV 没有这个设备
  
errno.ENOTDIR 不是一个目录
  
errno.EISDIR 这是一个目录
  
errno.EINVAL 参数无效
  
errno.ENFILE 文件表溢出
  
errno.EMFILE 打开文件过多
  
errno.ENOTTY 与文件描述符关联的不是终端
  
errno.ETXTBSY 文本文件正忙
  
errno.EFBIG 文件过大
 
errno.ENOSPC 设备空间不足
  
errno.ESPIPE Illegal seek
  
errno.EROFS 只读文件系统
  
errno.EMLINK 链接过多
  
errno.EPIPE 管道损坏
  
errno.EDOM 数学参数超过函数的值域
  
errno.ERANGE数学结果不可表示
  
errno.EDEADLK 资源死锁
  
errno.ENAMETOOLONG 文件名过长
  
errno.ENOLCK 无可用记录锁
  
errno.ENOSYS 函数未实现
  
errno.ENOTEMPTY 目录非空
  
errno.ELOOP 太多符号链接
  
errno.EWOULDBLOCK 操作将会阻塞
  
errno.ENOMSG 没有目标类型的消息
  
errno.EIDRM 标识符被删除
  
errno.ECHRNG Channel 数越界
  
errno.EL2NSYNC Level 2 未同步
  
errno.EL3HLT Level 3 停止
  
errno.EL3RST Level 3 重置
  
errno.ELNRNG Link 数越界
  
errno.EUNATCH Protocol driver not attached
  
errno.ENOCSI 没有CSI 结构可用
  
errno.EL2HLT Level 2 中止
  
errno.EBADE 无效的 exchange
  
errno.EBADR 无效的请求标识符
  
errno.EXFULL exchange 已满
  
errno.ENOANO No anode
  
errno.EBADRQC 无效请求码
  
errno.EBADSLT 无效 slot
  
errno.EDEADLOCK File locking deadlock error
  
errno.EBFONT 字体文件格式不合法
  
errno.ENOSTR 非流设备
  
errno.ENODATA 无可用数据
  
errno.ETIME 定时器过期  
  
errno.ENOSR 无可用流资源
  
errno.ENONET 机器不在网络上
  
errno.ENOPKG 包未安装
  
errno.EREMOTE 远程对象
  
errno.ENOLINK Link has been severed
  
errno.EADV 广播错误
  
errno.ESRMNT Srmount error
  
errno.ECOMM Communication error on send
  
errno.EPROTO 协议错误
  
errno.EMULTIHOP 尝试进行多跳(Multihop)
  
errno.EDOTDOT RFS specific error
  
errno.EBADMSG 不是数据消息
  
errno.EOVERFLOW 值过大
  
errno.ENOTUNIQ 在网络上的名称不唯一
  
errno.EBADFD 文件描述字异常
  
errno.EREMCHG 远程地址已变
  
errno.ELIBACC 无法访问一个需要的公共库
  
errno.ELIBBAD 正在访问一个受损的公共库
  
errno.ELIBSCN a.out 文件中的 .lib 端受损
  
errno.ELIBMAX 尝试链接太多的公共库
  
errno.ELIBEXEC 无法直接执行一个公共库  
  
errno.EILSEQ 非法字节序
  
errno.ERESTART 被打断的系统调用应该重启
  
errno.ESTRPIPE 流管道错误Streams pipe error
  
errno.EUSERS 用户过多
  
errno.ENOTSOCK 在非套接字上进行套接字操作
  
errno.EDESTADDRREQ 需要目的地地址  
  
errno.EMSGSIZE 消息过长
  
errno.EPROTOTYPE Protocol wrong type for socket
  
errno.ENOPROTOOPT 协议不可用
  
errno.EPROTONOSUPPORT 协议不支持
  
errno.ESOCKTNOSUPPORT 套接字类型不支持
  
errno.EOPNOTSUPP Operation not supported on transport endpoint
  
errno.EPFNOSUPPORT 协议族不支持
  
errno.EAFNOSUPPORT 协议不支持地址族
  
errno.EADDRINUSE 地址正在使用中
  
errno.EADDRNOTAVAIL 无法指定请求的地址
  
errno.ENETDOWN 网络已宕
  
errno.ENETUNREACH 网络不可达
  
errno.ENETRESET Network dropped connection because of reset
  
errno.ECONNABORTED 软件造成的连接中止
  
errno.ECONNRESET 连接由 peer 重置
  
errno.ENOBUFS无可用缓冲空间
  
errno.EISCONN Transport endpoint is already connected
  
errno.ENOTCONN Transport endpoint is not connected
  
errno.ESHUTDOWN Cannot send after transport endpoint shutdown
  
errno.ETOOMANYREFS 引用过多: cannot splice
  
errno.ETIMEDOUT 连接超时
  
errno.ECONNREFUSED 拒绝连接
  
errno.EHOSTDOWN 主机宕机
  
errno.EHOSTUNREACH 没有到达主机的路由
  
errno.EALREADY 操作已经进行
  
errno.EINPROGRESS 操作现在进行  
  
errno.ESTALE Stale NFS file handle
  
errno.EUCLEAN 结构体需要清洗  
  
errno.ENOTNAM 不是一个 XENIX具名文件
  
errno.ENAVAIL 没有 XENIX 信号量可用
  
errno.EISNAM 是一个具名文件
  
errno.EREMOTEIO 远程 I/O 错误
  
errno.EDQUOT 超过限额


引用:
http://www.runoob.com/python/python-exceptions.html
https://stackoverflow.com/questions/15304934/how-to-get-the-list-of-error-numbers-errno-for-an-exception-type-in-python
http://christophergs.github.io/python/2017/01/15/python-errno/
https://www.cnblogs.com/Security-Darren/p/4168392.html
https://docs.python.org/3/library/errno.html
https://docs.python.org/3.8/library/socket.html
https://stackoverflow.com/questions/5161167/python-handling-specific-error-codes
https://www.cnblogs.com/koliverpool/p/6791614.html

你可能感兴趣的:(Python)