pyton socket error

 

import socket

try:
    # 你的socket代码,例如创建一个连接
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('localhost', 8080))
except socket.timeout:
    print("Socket operation timed out.")
except OSError as e:
    if e.errno == socket.ECONNREFUSED:
        print("Connection refused by the server.")
    elif e.errno == socket.ECONNRESET:
        print("Connection reset by the server.")
    elif e.errno == socket.EHOSTUNREACH:
        print("Host is unreachable.")
    elif e.errno == socket.ENETUNREACH:
        print("Network is unreachable.")
    elif e.errno == socket.ETIMEDOUT:
        print("Connection timed out.")
    else:
        print(f"An unexpected OS error occurred: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    s.close()

 

 

#################

posted @ 2025-03-03 15:56  西北逍遥  阅读(22)  评论(0)    收藏  举报